0

这个程序说第一个有效是无效的,无论如何,然后第二个输入无论如何都是有效的。知道为什么吗?

public class test
    {
    public static void main (String [] args)
            {
            int gradeValidity, verifiedGrade, grade, validGrade;


            grade=gradeValidity ();

            System.out.println("your total grade is"+grade);
            System.exit(0);
            }
            private static int gradeValidity ()
            {
            int gradeOne;
            boolean verifiedGrade ;

            gradeOne=ConsoleInput.readInt("Input student grade");
            verifiedGrade=((gradeOne>=0) && (gradeOne<=100));

                            do
                            {
                            System.out.println("Invalid input");
                            gradeOne=ConsoleInput.readInt("Input student grade");
                            }
                            while (!verifiedGrade);



                            return gradeOne;
                            }

            }

将其更新为此。但现在我可以无与伦比(布尔和整数)

import io.*;
public class test
    {
    public static void main (String [] args)
            {
            int gradeValidity, verifiedGrade, grade, validGrade;


            grade=gradeValidity ();

            System.out.println("your total grade is"+grade);
            System.exit(0);
            }
            private static int gradeValidity ()
            {
            int gradeOne;
            boolean verifiedGrade ;

            gradeOne=ConsoleInput.readInt("Input student grade");
            verifiedGrade=((gradeOne>=0) && (gradeOne<=100));

                            while (gradeOne!=verifiedGrade);
                            {
                            System.out.println("Invalid input");
                            gradeOne=ConsoleInput.readInt("Input student grade");
                            }




                            return gradeOne;
                            }

            }

它仍然会出现错误,第一个输入始终为假,第二个输入始终为真,无论 >0 和 <100

import io.*;
public class test
    {
    public static void main (String [] args)
            {
            int gradeValidity, verifiedGrade, grade, validGrade;


            grade=gradeValidity ();

            System.out.println("your total grade is"+grade);
            System.exit(0);
            }
            private static int gradeValidity ()
            {
            int gradeOne;
            boolean  verifiedGrade;

            gradeOne=ConsoleInput.readInt("Input student grade");
            verifiedGrade=((gradeOne>=0) && (gradeOne<=100));

                            while (!verifiedGrade);
                            {
                                    System.out.println("Invalid input");
                                    gradeOne=ConsoleInput.readInt("Input student grade");
                                    verifiedGrade=((gradeOne>=0) && (gradeOne<=100));
                             }
                            return gradeOne;
                            }

            }
4

3 回答 3

1

导致此特定错误的行是这个

  boolean verifiedGrade;

您需要将此行更改为以下内容

  int verifiedGrade;

一切都会正常运行,直到您没有任何其他错误!

于 2013-10-15T05:28:15.813 回答
0

方法gradeValidity 应该是这样的:

private static int gradeValidity ()
{
    int gradeOne;
    boolean verifiedGrade;

    gradeOne=ConsoleInput.readInt("Input student grade");
    verifiedGrade=((gradeOne>=0) && (gradeOne<=100));

    while (!verifiedGrade)
    {
        System.out.println("Invalid input");
        gradeOne=ConsoleInput.readInt("Input student grade");
        verifiedGrade=((gradeOne>=0) && (gradeOne<=100));
    }

    return gradeOne;
}

编辑:也试试下面的这个代码片段。在其中,我用更多的标准类替换了非标准类 ConsoleInput。经过测试,它可以工作。

private static int gradeValidity () throws java.io.IOException
{
    int gradeOne;
    boolean verifiedGrade;
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Input student grade");
    gradeOne=Integer.parseInt(bufferRead.readLine());
    verifiedGrade=((gradeOne>=0) && (gradeOne<=100));

    while (!verifiedGrade)
    {
        System.out.println("Invalid input");
        System.out.println("Input student grade");
        gradeOne=Integer.parseInt(bufferRead.readLine());
        verifiedGrade=((gradeOne>=0) && (gradeOne<=100));
    }

    return gradeOne;
}
于 2013-10-15T06:47:42.007 回答
0
                while (gradeOne=!verifiedGrade)

应该

                while (gradeOne!=verifiedGrade)
于 2013-10-15T05:26:37.713 回答