如何不为循环内的无效输入递增循环计数器

时间:2014-08-25 01:35:05

标签: java for-loop

我试图编写一个简单的程序,它将使用扫描仪进行输入并输出最低数字,最高数字和平均值。但我不希望数字低于0或大于100.我已经有程序输出最小值,最大值和平均值,它会告诉我输入的数字是否小于0或大于100,但它仍然把它视为"旋转"在for循环中。我基本上希望程序返回到前一个数字,而不是在输入错误的数字时在for循环中继续前进。有什么建议? 谢谢!

import java.text.DecimalFormat;
import java.util.Scanner;
public class ExamGrades {

public static void main(String[]args){

    Scanner scan = new Scanner(System.in );

        int number = 0;
        double total = 0;
        int minimum;
        int maximum = 0;

        System.out.println("Please enter the first integer: ");

        number = scan.nextInt();
        minimum=number;
        total += number;

        for(int i = 2; i<11; i++){

            if(number<0 || number >100){
                System.out.println("Please enter a valid number: ");
                number = scan.nextInt();
            }
            else{
                System.out.println("Please enter integer " + i + ":");
                number = scan.nextInt();
                total += number;

            if(number<minimum)
                minimum = number;
            if(number>maximum)
                maximum = number;
            }
        }

        DecimalFormat oneDecimalPlace = new DecimalFormat("##.#");
        System.out.println("The minimum is: " + minimum);
        System.out.println("The maximum is: " + maximum);
        System.out.println("The average is: " + oneDecimalPlace.format((total) / 10.0 ));

    }
}

4 个答案:

答案 0 :(得分:1)

您可以从for循环内部递减循环计数器(变量i);这可能不是最好的做法,但它会完成工作。

或者,您可以使用while循环(例如while(i&lt; 11)并仅在输入可接受的值时递增i。

答案 1 :(得分:0)

考虑使用while()代替for()

int i = 1;
while(i <= 10){
    if(number < 0 || number > 10) {
        // Code goes here
    } else {
        // More code goes here
        i++; // Notice that i only increments here
    }
}

其他选项是do... while()循环,可以帮助您避免重复代码:

int i = 1;
do {
    if(i == 1)
        System.out.print("\nEnter first number:\t");
    else
        System.out.print("\nEnter next number: \t");
    number = scan.nextInt();
    if(number < 0 || number > 10) {
        // Code goes here
    } else {
        // More code goes here
        i++; // Again, i only increments here
    }
} while(i < 10);

答案 2 :(得分:0)

您可以将if语句更改为while loop

for(int i = 2; i < 11; i++){
    System.out.println("Please enter integer " + i + ":");
    number = scan.nextInt();

    while(number < 0 || number > 100){ // Wait for valid input
        System.out.println("Please enter a valid number: ");
        number = scan.nextInt();
    }

    total += number;
    if(number < minimum) minimum = number;
    if(number > maximum) maximum = number;
}

这样你就不会进入for loop中的另一次迭代,直到有效输入为止。

答案 3 :(得分:0)

将其写为while循环

import java.text.DecimalFormat;

import java.util.Scanner;

public class ExamGrades {

public static void main(String[]args){

    Scanner scan = new Scanner(System.in );

    int number = 0;
    double total = 0;
    int minimum;
    int maximum = 0;
    int I = 2;
    System.out.println("Please enter the first integer: ");

    number = scan.nextInt();
    minimum=number;
    total += number;

    while (I < 11){

        if(number<0 || number >100){
            System.out.println("Please enter a valid number: ");
            number = scan.nextInt();
        }
        else{
            System.out.println("Please enter integer " + i + ":");
            number = scan.nextInt();
            total += number;

            if(number<minimum)
                minimum = number;
            if(number>maximum)
                maximum = number;
            I++;
            }
        }

        DecimalFormat oneDecimalPlace = new DecimalFormat("##.#");
        System.out.println("The minimum is: " + minimum);
        System.out.println("The maximum is: " + maximum);
        System.out.println("The average is: " + oneDecimalPlace.format((total) / 10.0 ));

    }
}