java - 循环不完全工作java

标签 java

我是 stackoverflow 的新手。首先,我希望程序循环显示一个价格,然后是一个问题(输入另一个价格?),价格,然后是一个问题,依此类推。下面是输出。

Please enter a price: 
33
Enter another price? 
y
Please enter a price: 
66
Please enter a price: 
99
Please enter a price: 
22

但是最后它会继续循环显示“请输入价格:”。我希望它能做到:

Please enter a price: 
33
Enter another price? 
y
Please enter a price: 
66
Enter another price? 
y
Please enter a price: 
22

有人可以帮我解决这个问题吗?此外,有时平均值不会完全更新。谢谢:)

import java.util.Scanner;


public class ReadInPrice {

public static void main(String[] args) {

    int integer = 0;
    int count = 0; 
    double sum = 0;
    double average = 0; 
    Scanner input = new Scanner(System.in);
    String addPrice;

    System.out.println("Please enter a price: ");
    integer = input.nextInt();

    do { 
        System.out.println("Enter another price? ");
        addPrice = input.next();
        while (addPrice.equalsIgnoreCase("Y")) { // change this line to while user response = no etc may need a enter another number?
            count = count + 1;  
            sum = sum + integer; 
            System.out.println("Please enter a price: ");
            integer = input.nextInt();
        }

    }
    while (addPrice.equalsIgnoreCase("Y"));

    average = sum / count;
    System.out.println("Average = " + average);

    input.close();
}

}

最佳答案

您需要将 while 替换为 if

if (addPrice.equalsIgnoreCase("Y")) { // change this line to while user response = no etc may need a enter another number?
  count = count + 1;  
  sum = sum + integer; 
  System.out.println("Please enter a price: ");
  integer = input.nextInt();
}

事实上,addPrice 在第二个 while 循环中并未被修改,因此您有一个无限循环。

为了达到平均价格,你的方法是对的,但地方不对:P
count = count +1sum = sum + integer 应在每个 integer = input.nextInt() 之后完成。在当前代码中,您不会增加计数器,也不会为最后一个输入添加整数。

System.out.println("Please enter a price: ");
integer = input.nextInt();
count++ ; // count = count +1
sum += integer ; // sum = sum + integer
do { 
    System.out.println("Enter another price? ");
    addPrice = input.next();
    while (addPrice.equalsIgnoreCase("Y")) { // change this line to while user response = no etc may need a enter another number?
        System.out.println("Please enter a price: ");
        integer = input.nextInt();
        count++ ; // count = count +1
        sum += integer ; // sum = sum + integer
    }

}
while (addPrice.equalsIgnoreCase("Y"));

最后这是一个改进的版本,它避免使用 if

int sum = 0;
int integer = 0;
String addPrice = "Y";
while( "Y".equalsIgnoreCase(addPrice) ) {
   System.out.println("Please enter a price: ");
   integer = input.next();
   sum += integer ;
   count++;        
   System.out.println("Enter another price? ");
   addPrice = input.next();
}
int avg = sum / count ;

关于java - 循环不完全工作java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27445588/

相关文章:

java - Android studio firebase - 使用 UID 访问用户信息

Java移动开发

Java - 将对象传递给扩展类

java - 如何获取类文件的类参数

java - 没有继承可以实现多态吗?

java.lang.ClassNotFoundException : org. codehaus.jettison.json.JSONException

java - getWritableDatabase 不起作用

Java-Calculator 多计算

java - 无法理解 Apache 的文档

java - 无法解析原型(prototype) org.glassfish.jersey.archetypes :jersey quickstart-webapp