怎么办我让用户重新输入考试成绩,如果它是否定的,在第二个for循环?

时间:2014-10-03 23:57:55

标签: java for-loop input calculator negative-number

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

            }



    }




}

在第二个for循环中,我似乎无法弄清楚如果用户是负面的,如何让用户重新输入他/她的考试分数输入。这是我到目前为止所做的。

1 个答案:

答案 0 :(得分:1)

   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");

            }

        }

    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;

        }