简体   繁体   English

在第二个for循环中,如何使用户重新输入考试分数(如果为负)?

[英]How to do I make the user re-enter the exam scores if its negative, in the second for loop?

import java.util.Scanner;

public class GradeCalculator {

    public static void main(String[] args) {

        double examAdd = 0;
        double examAvg = 0;
        double exam    = 0;
        int j          = 0;

        Scanner s = new Scanner(System.in);

            System.out.print("Please enter the number of students: ");
            int stuNum = s.nextInt();

            System.out.print("Please enter the number exams      : ");
            int examNum = s.nextInt();


            for (int i = 1; i <= stuNum; i++)  { 
                Scanner readName = new Scanner(System.in); /* had to re-make another scanner, had issues without one inside the for loop JUST with "nextLine()" nothing else*/

                System.out.println("\n--------------------------------------");

                System.out.print("Enter student " + i + "'s name : " );
                String name = readName.nextLine();

                System.out.print("Enter exam scores      : ");

                for (j = 0; j < examNum; j++) {
                    exam = s.nextDouble();
                        examAdd = (examAdd + exam);

                }   
                        examAvg = (examAdd/examNum);

                        System.out.println("Grade Statistics for " + name);
                        System.out.println("  Average     : " + examAvg);

                        if (examAvg <= 100 & examAvg >= 90){
                            System.out.println ("  Letter Grade:  A \n" + "  " + name + " gets 4 stars! ****\n");
                            examAvg = 0; /* Restart the average formula to calculate next students average*/
                            examAdd = 0;
                        }
                        else if (examAvg <= 89.99 & examAvg >= 80){
                            System.out.println("  Letter Grade: B \n" + "  " + name + " gets 3 stars! ***\n");
                            examAvg = 0; /* Restart the average formula to calculate next students average*/
                            examAdd = 0;
                        }
                        else if (examAvg <= 79.99 & examAvg >= 70){
                                System.out.println("  Letter Grade: C \n" + "  " + name + " gets 2 stars! **\n");
                                examAvg = 0; /* Restart the average formula to calculate next students average*/
                                examAdd = 0;
                            }
                        else if (examAvg <= 69.99 & examAvg >= 60){
                                System.out.println("  Letter Grade: D \n" + "  " + name + " gets 1 star! *\n");
                                examAvg = 0; /* Restart the average formula to calculate next students average*/
                                examAdd = 0;
                            }
                        else if (examAvg <= 59.99) {
                            System.out.println("  Letter Grade: F \n" + "  " + name + " gets 0 stars!\n");
                            examAvg = 0;/* Restart the average formula to calculate next students average*/
                            examAdd = 0;
                        }

            }



    }




}

In the second for loop, I can not seem to figure out how to make the user re-enter his/her inputs for exam scores if it is negative. 在第二个for循环中,我似乎无法弄清楚如何使用户重新输入他/她的输入以获取考试分数(如果为负)。 This is what I have so far. 到目前为止,这就是我所拥有的。

   for (j = 0; j < examNum; j++) {
        exam=-1
        while (exam<0){

            exam = s.nextDouble();
                examAdd = (examAdd + exam);
            if (exam<0) 
                System.out.println("negative write again");

            }

        }

or 要么

    for (j = 0; j < examNum; j++) {

        while (true){

            exam = s.nextDouble();
                examAdd = (examAdd + exam);
            if (exam<0) 
                System.out.println("negative write again");

            else 
                continue;

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在用户登录失败时重新进入 - How to make it re-enter when user login fail 我如何要求用户重新输入他们的选择? - How can I ask the user to re-enter their choice? 如果值不正确,使用户重新输入值 - make a user re-enter values if value not correct 当要求用户在Java中循环输入一些字符串时,如何仅保留文本打印一次 - How to keep Text print only once when asking a user to re-enter some string in loop in Java 如果用户输入参数之外的数字,如何循环提示用户重新输入数字 - How to loop a prompt for a user to re-enter a number if they input a number outside of the parameters 编写一个包含 while 循环代码的程序来计算用户输入的考试分数数量 - Write a program with while loop code in it to calculate the amount of exam scores enter by a user 提示用户重新输入整数直到输入二进制数 - Prompting user to re-enter integers until binary number is entered Java-异常处理-如何重新输入无效输入 - Java - Exception handling - How to re-enter invalid input 如果发生错误,如何提示用户重新输入值,以及如何提供继续检查工资率或退出程序的选项? - How to prompt the user to re-enter a value if a mistake was made and How to give an option to keep on checking pay rates or to exit the program.? 第一次进入活动时,RecyclerView没有填充,但退出并重新进入活动后显示 - RecyclerView not populating the first time I enter the activity but showing after i exit and re-enter the activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM