循环不太工作java

时间:2014-12-12 14:18:44

标签: 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

然而,它会在#34结束时保持循环;请输入价格:"。我希望它能做到:

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();
}

}

4 个答案:

答案 0 :(得分:3)

您需要将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 ;

答案 1 :(得分:0)

我想你想要这样的东西:

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 = "Y";


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

input.close();
}

}   

答案 2 :(得分:0)

你应该做的是改变你的逻辑。您需要重复两个操作,输入价格并询问用户是否要输入其他价格。这只需要一个循环。

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

    count = count + 1;
    sum = sum + integer;

    System.out.println("Enter another price? ");
    addPrice = input.next();
} while (addPrice.equalsIgnoreCase("Y"));

答案 3 :(得分:0)

试试这个:

编辑添加了最小值和最大值

    public class ReadInPrice {

    public static void main(String[] args) {
        //better to use 2 scanners when dealing with both string and int
        //one for string ; one for ints
        Scanner strScanner = new Scanner(System.in);
        Scanner intScanner = new Scanner(System.in);
        boolean enter = true;
        int sum = 0;
        int count = 0;
        int min=Integer.MAX_VALUE;
        int max=0;

        while (enter) { //while user wants to keep adding numbers
            System.out.println("Please enter a price: ");
            int price = intScanner.nextInt();

            if(price < min)
                min=price;

            if(price > max)
                max=price;

            sum += price;
            count++;
            System.out.println("Enter another price? ");
            String answer = strScanner.nextLine();
            if (!answer.equalsIgnoreCase("Y"))
                enter = false; //user doesn't want to keep adding numbers - exit while loop
        }
        double average = (double)sum / count;
        System.out.println("Average = " + average);
        System.out.println("Min = " + min);
        System.out.println("Max = " + max);
        strScanner.close();
        intScanner.close();
        System.exit(0);
    }
}