2014-11-06 119 views
0

我有一個程序,用於處理在不同方法中使用不同的循環。除了調用另一個方法之外,整個程序運行良好。將一種方法調入另一種方法的麻煩

我遇到了調用另一個方法的問題。

我想要做的是:

  1. 有方法撥打對方並運行它從用戶(1 - 100)採用可變 。
  2. 如果輸入了無效輸入 ,則增加一個變量。
  3. 當連續輸入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); 
} 
+1

這是否甚至編譯? 。對於初學者來說,processGrade(percentMark)有一個void返回類型而不是boolean,但你期望返回一個布爾值?布爾結果= processGrade(百分號) – 2014-11-06 15:42:33

+0

對不起,這是一個粘貼錯誤。我編輯了代碼。整個代碼會有幫助嗎?我知道很多人討厭任何人發佈過多的代碼。這是3班,不是太多。 – DarkBlueMullet 2014-11-06 15:44:11

+1

信息:這裏不需要標籤。儘量避免它們。爲什麼你叫'processGrade(percentMark)'兩次?仍然是「粘貼錯誤」? – Tom 2014-11-06 15:46:11

回答

2
processGrade(percentMark);// Call processGrade method 

會在這裏你造成的問題,刪除它。

您已將processGrade(percentMark);結果賦值給result變量。

+1

在退出do/while循環之前,第二次調用會導致輸入數量增加。 (就像一個addtitional信息) – Tom 2014-11-06 15:52:56

+0

就是這樣......就這麼簡單。非常感謝你!我感到很愚蠢,因爲沒有意識到將類分配給變量實際上也是在調用它。學到了新的東西,再次感謝你! – DarkBlueMullet 2014-11-06 15:53:41

2

我會刪除boolean result = processGrade(percentMark),而是改變你的if語句:

if(!processGrade(percentMark)){ 
    invalid++; 
} 
相關問題