【问题标题】:How to store the data on every instance of do-while loop in Java?如何在 Java 中的每个 do-while 循环实例上存储数据?
【发布时间】:2020-05-26 12:39:24
【问题描述】:

我目前正在学习 Java,我需要在我的 do-while 循环中添加每个实例的值。有什么方法可以存储这些值,这样就不会在每个循环中都被覆盖?

import java.util.Scanner;   

class Main{
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    char userChar;

    do {

        System.out.println("Apples are $10");
        System.out.println("How many do you want?");
        int itemQty = input.nextInt();

        System.out.println("Do you wish to buy more? (y/n)");
        userChar = input.next()charAt(0);

    } while (userChar == 'y');

    // all values entered by the user needs to be added 
    System.out.println("The total is: $" + (itemQty*10)); 

}

}

【问题讨论】:

  • int itemQty移出循环,然后写itemQty += input.nextInt(),这样您就可以创建一个可以在最后打印的苹果总和
  • 困扰我好几个小时了!!它现在可以工作了,非常感谢伙计!!!

标签: java loops java.util.scanner store do-while


【解决方案1】:
public class Main {

     public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    char userChar;

        int itemQty=0;
        int totalQty=0;
    do {

        System.out.println("Apples are $10");
        System.out.println("How many do you want?");

        itemQty = input.nextInt();
        totalQty+=itemQty;
        System.out.println("Do you wish to buy more? (y/n)");
        userChar = input.next()charAt(0);

    } while (userChar == 'y');

    // all values entered by the user needs to be added 
    System.out.println("The total is: $" + (itemQty*10)); 
}
}

按照 Lino 的建议,将 itemQty 保持在循环之外并将其初始化为零。

您还想为每个循环迭代存储 itemQty 的值吗?

如果是,则使用 ArrayList。

ArrayList<int> valuesList = new ArrayList<int>();

并将循环代码更改为

int itemQty=0;
int totalQty=0;
do {

    System.out.println("Apples are $10");
    System.out.println("How many do you want?");
    itemQty = input.nextInt();
    valuesList.add(itemQty*10);//New line to be added
    totalQty += itemQty;
    System.out.println("Do you wish to buy more? (y/n)");
    userChar = input.next().charAt(0);

} while (userChar == 'y');

然后在循环结束后,显示每个阶段的值。

for (int i = 0; i < valuesList.size(); i++) {            
      System.out.println("The value of Qty in stage "+(i+1)+" is $"+valuesList.get(i));       
  } 

这将是你的最终输出

苹果是 10 美元 你想要多少? 10 你想买更多吗? (是/否) 是的 苹果是 10 美元

你想要几个?

5

你想买更多吗? (是/否)

n

总计:150 美元

第一阶段Qty的价值是$100

第 2 阶段的 Qty 价值为 $50

【讨论】:

  • 如果可能的话,是的
【解决方案2】:

您可以在循环外为 totalSum 定义一个变量,然后每次用户输入一个数字时,将 itemQty 添加到它。

int totalSum = 0;
do {
    ...
    int itemQty = input.nextInt();
    totalSum += itemQty;
    ...
} while (...);
// Here totalSum is the sum of all user inputs

【讨论】:

    【解决方案3】:

    你可以像下面这样更新你的程序 -

    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);
    
        char userChar;
        int itemQty = 0;
    
        do {
    
            System.out.println("Apples are $10");
            System.out.println("How many do you want?");
    
            itemQty += input.nextInt();
    
    
            System.out.println("Do you wish to buy more? (y/n)");
            userChar = input.next().charAt(0);
    
        } while (userChar == 'y');
    
        // all values entered by the user needs to be added
        System.out.println("The total is: $" + (itemQty*10));
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 1970-01-01
      • 2020-11-26
      • 2010-12-03
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      相关资源
      最近更新 更多