我有一個程序,用於處理在不同方法中使用不同的循環。除了調用另一個方法之外,整個程序運行良好。將一種方法調入另一種方法的麻煩
我遇到了調用另一個方法的問題。
我想要做的是:
- 有方法撥打對方並運行它從用戶(1 - 100)採用可變 。
- 如果輸入了無效輸入 ,則增加一個變量。
- 當連續輸入3個輸入爲 時,顯示消息並退回到菜單。
被調用的方法在它自己的工作正常,它可以工作,當我打電話。
我無法工作的是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);
}
這是否甚至編譯? 。對於初學者來說,processGrade(percentMark)有一個void返回類型而不是boolean,但你期望返回一個布爾值?布爾結果= processGrade(百分號) – 2014-11-06 15:42:33
對不起,這是一個粘貼錯誤。我編輯了代碼。整個代碼會有幫助嗎?我知道很多人討厭任何人發佈過多的代碼。這是3班,不是太多。 – DarkBlueMullet 2014-11-06 15:44:11
信息:這裏不需要標籤。儘量避免它們。爲什麼你叫'processGrade(percentMark)'兩次?仍然是「粘貼錯誤」? – Tom 2014-11-06 15:46:11