如何在for循环中设置限制?

时间:2017-04-12 00:50:41

标签: java eclipse for-loop while-loop

我必须创建一个程序来计算每个学生的平均值。分数。我设法做到了,但我怎么能将得分限制在0到100之间?我已经搜索了其他问题和许多节目,以便发表声明。问题是我不知道在哪里添加。所以这里是代码:

import java.util.Scanner;

public class AverageScore {
public static void main(String[] args) {

    int x; // Number of students
    int y; // Number of tests per student
    int Score = 0; //Score of each test for each student
    double Average = 0; //Average score
    double Total = 0; //Total score

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please enter the number of students: ");
    x = keyboard.nextInt();

    System.out.println("Please enter the amount of test scores per student: ");
    y = keyboard.nextInt();

    for (int z = 0; z < x; z++)
    {           
    System.out.println("Student " + (z + 1));
    System.out.println("------------------------");

    for (int g=0; g < y; g++)
    {
        System.out.print("Please enter score " + (g + 1) + ":  ");

        Total += new Scanner(System.in).nextInt();
        Total += Score;
        Average = (Total/y);
    }       

    System.out.println("The average score for student " + (z + 1) + " is " + Average);
    System.out.println("                      ");

    Total= 0;
    }

    keyboard.close();
}
}

如果还有其他方法,请说明。提前谢谢。

2 个答案:

答案 0 :(得分:1)

一个简单的方法是将用户输入提示放在while循环中,并且只有在您确认该等级有效后才会中断:

Scanner scanner = new Scanner(System.in);

int score;

while (true) {
    System.out.print("Please enter score " + (g + 1) + ":  ");

    score = scanner.nextInt();

    if (score >= 0 && score <= 100) {
        break;
    }

    System.out.println("Please enter a valid score between 0 and 100!");
}

Total += score;

请记得关闭Scanner以避免内存泄漏!

答案 1 :(得分:1)

import java.util.Scanner;

public class AverageScore {
public static void main(String[] args) {

    int x; // Number of students
    int y; // Number of tests per student
    int Score = 0; //Score of each test for each student
    double Average = 0; //Average score
    double Total = 0; //Total score
    double Input = 0; **//Add this in your variable**
    boolean Valid = false; **//Add this in your variable**

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter the number of students: ");
        x = keyboard.nextInt();

        System.out.println("Please enter the amount of test scores per student: ");
        y = keyboard.nextInt();

        for (int z = 0; z < x; z++)
        {
            System.out.println("Student " + (z + 1));
            System.out.println("------------------------");

            for (int g=0; g < y; g++)
            {
                System.out.print("Please enter score " + (g + 1) + ":  ");
                Input = new Scanner(System.in).nextInt();

                //validation of your input from 0 to 100
                if(Input>=0&&Input<=100)
                {
                    Valid = true;
                }

                //enter while loop if not valid
                while(!Valid)
                {
                    System.out.println("");
                    System.out.print("Please enter a valid score " + (g + 1) + ":  ");
                    Input = new Scanner(System.in).nextInt();
                    if(Input>=0&&Input<=100)
                    {
                        Valid = true;
                    }
                }

                Valid = false; //reset validation;

                Total += Input;
                Average = (Total/y);
            }       

            System.out.println("The average score for student " + (z + 1) + " is " + Average);
            System.out.println("                      ");

            Total= 0;
        }

        keyboard.close();
    }
}