我有一个程序可以处理在不同方法中使用不同的循环。除了一种调用另一种方法的方法之外,整个程序工作正常。
我遇到一个调用另一个方法的问题。
我想做的是:
被调用的方法可以正常工作,并且在我调用它时可以正常工作。
我无法工作的是3次无效输入后的显示消息。目前,它需要大约7个无效输入,然后显示消息??
有效的方法:
public static boolean processGrade(int percentMark)
{
Menu m = new Menu();
clrscr();
System.out.println("Please enter your mark e.g. 90. (input -1 to exit): ");
percentMark = Genio.getInteger();
if
(percentMark >=70 && percentMark <=100)
{
clrscr();
System.out.println("Your Grade is A - Excellent!\n\n");
pressKey();
clrscr();
}
else if(percentMark >= 60 && percentMark <70)
{
clrscr();
System.out.println("Your Grade is B - Good!\n\n");
pressKey();
clrscr();
}
else if (percentMark >=50 && percentMark <60)
{
clrscr();
System.out.println("Your Grade is C - Ok!\n\n");
pressKey();
clrscr();
}
else if(percentMark >=40 && percentMark <50)
{
clrscr();
System.out.println(" Your Grade is D - Must Do Better!\n\n");
pressKey();
clrscr();
}
else if (percentMark <40 && percentMark >= 0 )
{
clrscr();
System.out.println(" Your Grade is E - Must Do Better!\n\n");
pressKey();
clrscr();
}
else if (percentMark < -1 || percentMark >100)
{
clrscr();
System.out.println("ERROR: Value MUST be in the range of 0 - 100!");
pressKey();
clrscr();
return false;
}
else if (percentMark == -1)
{
//clrscr();
System.out.println("You entered -1, you will now return to the menu!");
pressKey();
return false;
}
return true;
}
我无法开始调用上述消息的方法:
public static void processGradeV2(int percentMark)
{
int invalid = 0;
outerloop:
do {
clrscr();
boolean result = processGrade(percentMark);
processGrade(percentMark);// Call processGrade method
if(result == false)
{
invalid++;
}
if(invalid == 3)
{
clrscr();
System.out.println("Sorry, you have entered an invalid integer 3 times in a row! The program will return to the menu screen.");
pressKey();
break outerloop;
//return;
}
if(percentMark == -1)
{
clrscr();
System.out.println("You entered -1, you will now return to the menu!");
pressKey();
clrscr();
break outerloop;
//processUserChoices();
}
}
while(invalid <3);
}
答案 0 :(得分:2)
processGrade(percentMark);// Call processGrade method
会在这里造成问题,请将其删除。
您已将processGrade(percentMark);
结果分配给result
变量。
答案 1 :(得分:2)
我会删除boolean result = processGrade(percentMark)
,而是将您的if语句更改为:
if(!processGrade(percentMark)){
invalid++;
}