这个程序说第一个有效是无效的,无论如何,然后第二个输入无论如何都是有效的。知道为什么吗?
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;
}
}
将其更新为此。但现在我可以无比(布尔和int)
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;
}
}
它仍然会出现错误,第一个输入始终为false,第二个输入始终为true,无论&gt; 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;
}
}
答案 0 :(得分:1)
导致此特定错误的行是
boolean verifiedGrade;
您需要将此行更改为以下
int verifiedGrade;
一切都会正常工作当然,除非你没有任何其他错误!
答案 1 :(得分:0)
while (gradeOne=!verifiedGrade)
应该是
while (gradeOne!=verifiedGrade)
答案 2 :(得分: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;
}