2016-10-02 39 views
-1

我想要問問四位參賽者的殘疾人隊伍,然後得到總數,如果分數大於32,那麼這是非法的,那麼它是合法的。所有這些都必須在For循環中使用並使用多個方法。下面列出了代碼 。java將不會執行第二種方法

public class Runner11 { 
    public static void main(String[] p) { 
     int Points; 
     int total = DisabilityClass(); 
     int Runner1; 
     int Runner2; 
     int Runner3; 
     int Runner4; 
     System.exit(0); 
    } 

    public static int DisabilityClass() { 
     Scanner Scanner = new Scanner(System.in); 
     System.out.println("What is the disability class of Runner 1?"); 
     int Runner1 = Scanner.nextInt(); 
     System.out.println("What is the disability class of Runner 2?"); 
     int Runner2 = Scanner.nextInt(); 
     System.out.println("What is the disability class of Runner 3?"); 
     int Runner3 = Scanner.nextInt(); 
     System.out.println("What is the disability class of Runner 4?"); 
     int Runner4 = Scanner.nextInt(); 
     int total = Runner1 + Runner2 + Runner3 + Runner4; 
     return total; 
    } 

    public static void Points(int total) { 
     int i; 
     for(i=32; i >= total; i++) { 
      System.out.println("That team has "+total+" points so it's legal"); 
     } 
     return; 
    } 
} 
+2

您需要在'int total = Runner1 + Runner2 + Runner3 + Runner4;'後面調用'Points(total)'並移除此行'System.exit(0);'這會殺死您的JVM,優雅地關閉你的JVM並從main方法中刪除Runner1等的聲明。 – SMA

+2

'for(i = 32; i> = total; i ++)'。再看一遍。你甚至不需要那個循環。 – Tunaki

回答

0

您不需要四個單獨的跑步者,也可以刪除System.exit(0)。你也在你的殘障班申報第二組參賽者。刪除所有跑步者,並嘗試在您的殘疾類內的代碼

int runner = 0; 
int total = 0; 

for(int i = 0; i<4; i++){ 
    System.out.println("What is the disability class"); 
    runner = sc.nextInt(); 
    total +=runner; 
} 

在您的積分類中使用此代碼。

if(total<=32){ 
     System.out.println("That team has "+total+" points so it's legal") 
}else{ 
     System.out.println("That team has "+total+" points so it's illegal"); 
} 

所有這些都可以在主要方法中完成,但是如果您必須使用單獨的方法。只需將代碼粘貼在那裏。

相關問題