简体   繁体   中英

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. 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;

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM