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

标签 java eclipse for-loop while-loop

我必须创建一个程序来计算每个学生的平均分。我设法做到了,但是如何将分数限制在 0 到 100 之间呢?我搜索了其他问题和许多节目来放置 while 语句。问题是我不知道在哪里添加 while。所以这是代码:

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

如果还有其他方法请注明。提前致谢。

最佳答案

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

关于java - 如何在for循环中设置限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43358470/

相关文章:

java - 如何使用java在整个屏幕上平滑移动鼠标?

java - 如何将混合类型文件中的整数存储到数组中? java

java - 为什么 BufferedOutputStream 中的字节转换没有达到预期效果?

java - 不使用 Intent 将事件插入日历错误。安卓工作室

C: 只需在 for 循环内调用 getline 即可将变量变为 0

python - 如何防止我的 FOR 循环过早结束?

python - for循环: iterating over one value of a list at a time in Python

java - 类型安全整数的设计模式?

sql - 有没有适用于 Eclipse 的 SQL 格式化插件?

android - 无法安装此硬件 - Android 硬件设备设置