当前位置: 首页 > 知识库问答 >
问题:

使用增强的for循环打印来自用户的输入并更新变量

漆雕育
2023-03-14

每当用户输入我的静态变量total的输入时,它不会通过使用=向它添加下一个输入来更新,相反,它会将这两个输入相加,得到两倍的数量。我想到了在增强的for循环下使用常规的for循环来迭代静态变量,但是我也没有成功。

    while(isRunning) {
      try {
        System.out.println("Please enter the name of your item you want to add: (type quit to exit) ");
          String item = input.nextLine();

        if(item.equalsIgnoreCase("quit")) {
          break;
        }
        System.out.println("Please enter the price of the item to be added to the invoice. ");
          receipt.setTotal(input.nextDouble());
          input.nextLine();
          itemList itemlist = new itemList(item, Invoice.total);
          price.add(itemlist);

      } catch (InputMismatchException e) {
        System.out.println("Exception caught!");
      }

      for(Invoice itemization : price) {
        Invoice.total += itemization.getTotal();
        System.out.println();
        System.out.println("Item: " + itemization.getItem() + "\nPrice: " + itemization.getTotal());
        System.out.println("Subtotal: " + Math.round(receipt.getTotal() * 100.00) / 100.00);

}



import java.util.*;

public class Invoice {

  static Double total;
  public String item;
  List<Invoice> price = new ArrayList<>();

  public String getItem() {
    return item;
  }

  public double getTotal() {
    return total;
  }

  public void setTotal(Double total) {
    Invoice.total = total;
  }

  public void endBalance() {

    if (getTotal() > 0.0) {
    System.out.println();
    System.out.println("$" + getTotal() + " Will be deposited once cleared. ");
    System.out.println();
    System.out.println("Expect most transfer to deposit to your account with 3 to 5 business days. ");
    System.out.println();

    } else {
      System.out.println("Available balance too low for bank deposit. ");
    }
  }

  // @Override
  // public String toString() {
  // //   return receipt.toString;
  // }
}

class itemList extends Invoice {

  public itemList(String item, Double total) {
    this.item = item;



  }

}

共有1个答案

公羊兴文
2023-03-14

因为InVoicegetTotal()方法返回静态变量的值,Invoice.total=itemization.getTotal();实际上是一个Invoice.total*=2;。你期望什么?

 类似资料:
  • 需要一些专家的建议。首先,这里是我的代码: 我的问题是:当我输入一个用户号码如“3”时,提示我的代码只打印一个__当我希望它打印指定的号码。我无论如何也想不出来。 我是英语专业的,所以这不是我的强项:(形状应该是菱形的。)

  • 问题内容: 我看到了如下一行代码: 就我所知,我认为要能够以这种格式编写for循环,我们需要将“ words”作为实现Iterable接口并覆盖iterator()函数的类的实例。但是’words’是String数组类型,这对于循环格式如何正确? 有人可以给我一些提示吗? 问题答案: 从有关此主题的Java教程中: for-each构造也适用于数组,其中它隐藏索引变量而不是迭代器。以下方法返回in

  • 问题内容: 我正在从Java切换到C ,并且想知道C 是否包含我在Java中使用的增强的for循环,例如: 在C ++中是否可能有相同的“快捷方式”? 问题答案: 在C ++ 11中,如果编译器支持,则可以。这称为基于范围的。 它适用于C样式数组以及具有函数并返回迭代器的任何类型。例:

  • 问题内容: 在玩循环的同时创建了以下代码。下面的代码将斐波那契值存储到数组中,然后使用for循环将其打印出来。 上面的代码工作正常。但是,第一次将它们放在一起时,我使用了增强的for循环来打印出值(代码中的第二个for循环)。可以很好地编译,但是运行时得到以下信息: 我不明白出了什么问题。更改第二个循环不应更改值(您会注意到斐波那契值是错误的(即缺少值))。而且我不明白为什么简单的增强型for循环

  • 问题内容: 我读到 增强的for循环 比普通的 for循环 更有效: http://developer.android.com/guide/practices/performance.html#foreach 当我搜索它们的效率之间的差异时,我发现的是:如果是普通的for循环,我们需要一个额外的步骤来找出数组的长度或大小等, 但这是唯一的原因,增强的for循环优于普通的for循环吗?在那种情况下,

  • 所以我的理解是,增强for循环应该更慢,因为它们必须使用迭代器。。然而,我的代码提供的结果参差不齐。。(是的,我知道循环逻辑占用了循环中的大部分时间) 对于较少的迭代次数(100-1000),无论有无JIT,增强的For循环似乎都要快得多。相反,对于大量迭代(100000000),传统循环速度要快得多。这是怎么回事?