【问题标题】:How to set a limit in a for loop?如何在for循环中设置限制?
【发布时间】:2017-04-12 00:50:40
【问题描述】:

我必须创建一个程序来计算每个学生的平均分数。我设法做到了,但我怎样才能将分数限制在 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();
}
}

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

【问题讨论】:

  • 假设您需要验证输入的测试分数为 0-100。那么就不需要while了,只要放一个if else子句来检查输入的分数是否在范围内。
  • 是的,这就是问题我不知道在哪里放置 if else
  • 您必须在要求输入分数之前输入它。抛开问题,你为什么要问两次分数?在第二个系统 out 和 g 的 for 循环中?
  • @jace 第一个问题是询问每个学生有多少考试。假设有 3 个学生,每个学生都有 3 个测试。因此3分。然后下一个是询问每个测试的分数是多少
  • 哦,好吧 :) 让我为它编写代码。谢谢你说清楚

标签: java eclipse for-loop while-loop


【解决方案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();
    }
}

【讨论】:

    【解决方案2】:

    解决此问题的一种简单方法是将用户输入提示放入 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;
    

    记得关闭你的Scanners 以避免内存泄漏!

    【讨论】:

    • @OusmaneMahyDiaw 哦?如果成绩无效,我为什么要跳出循环?
    • @OusmaneMahyDiaw 条件与break语句匹配,表示输入正确时跳过
    • @OusmaneMahyDiaw 别担心;它发生在我们最好的人身上!
    猜你喜欢
    • 2019-01-23
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多