0

扫描器不会在嵌套在 for 循环中的第二个 while 语句中提示用户输入。为什么?有什么解决办法吗?

   import java.util.Scanner;

    public class ComputeGpa
    {
        public static void main(String args[])
        { 
            char grade = '\0';
            int credits = 0;
            String aGrade = "";

            Scanner in = new Scanner(System.in);

            Gpa gpas = new Gpa();

            int numCourses = 0;
            while(numCourses <= 0)
            {
                System.out.print("Enter number of courses: ");
                numCourses = in.nextInt();
                if(numCourses <= 0)
                {
                    System.out.println("Invalid number of courses - must be greater than 0!");
                }
            }

            for(int i = 0; i < numCourses; i++)
            {
                while(aGrade.length() != 1)
                {
                    System.out.print("Enter grade (one character): ");
                    aGrade = in.next();
                    grade = aGrade.charAt(0);
                    if(aGrade.length() != 1)
                    {
                        System.out.println("Invalid grade - must be exactly one character");
                    }
                }

                while((credits < 0) || (credits > 9))// this is the while statement I have
                    problems with
                    {
                    System.out.print("Enter credits: ");
                    credits = in.nextInt();
                    if((credits <  0) || (credits > 9))
                    {
                        System.out.println("Invalid credits - must be between 0 and 9");
                    }
                    gpas.addTotals(grade, credits);
                    }
            }

            System.out.printf("GPA: %.2f", gpas.calcGpa()); 

        }        
    }

我要验证的条件是用户必须输入一个介于 0 和 9 之间的数字,包括 0 到 9。

然后我得到“NaN”的计算值

4

4 回答 4

1

Change your while loop to a do ... while loop, because you want the loop ALWAYS to run on the first time through, but you also want the condition to be checked at the end of the loop.

于 2013-10-29T03:22:33.737 回答
0

改变

while((credits < 0) || (credits < 9))

while((credits < 0) || (credits > 9))

旁注:“包含”意味着您希望将 0 和 9 作为允许的信用值包括在内。但这不是您在这里所拥有的,因为您使用的是 < 而不是 <= 和 > 而不是 >=。

于 2013-10-29T03:15:50.790 回答
0

将其更改为 while((credits <= 0) || (credits > 9)) 因为您已将 credits 初始化为“0”。只需添加此

 while((credits <= 0) || (credits > 9))// this is the while statement I have
                                              //problems with
      {
        System.out.print("Enter credits: ");
        credits = in.nextInt();
        if((credits <  0) || (credits > 9))
        {
            System.out.println("Invalid credits - must be between 0 and 9");
        }
        if(credits == 0)
            break;

     //   gpas.addTotals(grade, credits);
      }

credits 将接受 0 - 9 并且不会再次询问用户

于 2013-10-29T03:19:26.033 回答
0

I tried to modify last while loop to suit your needs , hope this code segment helps you.

do {
    System.out.print("Enter credits: ");
    credits = in.nextInt();
    if ((credits < 0) || (credits > 9)) {
    System.out.println("Invalid credits - must be between 0 and 9");
    }else{
        gpas.addTotals(grade, credits);
        }
} while ((credits < 0) || (credits > 9));
于 2013-10-29T03:30:47.337 回答