2017-05-28 43 views
1

我需要在每輪之後總結積分。當我這樣做時,它不起作用。它只是在if語句下輸出點系統。幫助和解釋會非常有幫助!感謝您從簡介Java編碼器。如何在循環中計算積分?

public class J1 { 
    public static void main(String args[]) { 
     // create random object 
     java.util.Random rand = new java.util.Random(); 
     java.util.Scanner scanner = new java.util.Scanner(System.in); 

     // check next int value 
     System.out.println("Wall height: " + rand.nextInt(10)); 
     double height = rand.nextInt(10); 

     System.out.println("Distance from wall: " + rand.nextInt(20)); 
      double dist = rand.nextInt(20); 

     for(int i = 1; i > 0; i++) { 

      System.out.println("Set a lanuch angle between 0 and 90: "); 
       double angle = scanner.nextDouble(); 
      System.out.println("Set a lanuch speed: "); 
       double speed = scanner.nextDouble(); 

      double point; 
      double a; 
      double b; 
      double c; 
      double d; 
      //double e; 
      double y; 
      a = dist*(Math.tan(Math.toRadians(angle))); 
      b = 9.81*(dist*dist); 
      c = (speed * Math.cos(angle)) * (speed * Math.cos(Math.toRadians(angle))); 
      d = 2*c; 
      y = a - (b/d); 
      System.out.println("Your max height is " +y+ " high"); 

      double space; 
      space = height - y; 

      if(space <= 3 && space > 0) { 
       System.out.println("You just made it! "); 
       point = 0 - 1 + 3; 
       System.out.println("You have " +point+ " points."); 
      } 

      if (space > 3 && space <= 6) { 
       System.out.println("Aww. Plenty of room!"); 
       point = 0 - 1 + 5; 
       System.out.println("You have " +point+ " points."); 
      } 

      if(space <= 0 && space >= -3) { 
       System.out.println("So close!"); 
       point = 0 - 1 - 2; 
       System.out.println("You have " +point+ " points."); 
      } 

      if (space < -3) { 
       System.out.println("Terrible aim!"); 
       point = 0 - 1 - 4; 
       System.out.println("You have " +point+ " points."); 
      } 
     }  

     System.out.println("Your total points: " +point); 
    } 
} 
+2

也許你想你'for'循環開始前宣佈'point',而不是裏面的'for'循環,這樣你就不會每次都得到一個新的變量。然後,每次分配給'point'時,嘗試使用'+ ='而不是'=',以便增加'point'的值,而不是重新分配它。 –

回答

2

point變量的聲明將需要的for循環之外發生爲它是用於在代碼的末尾打印訪問。

它也出現for(int i = 1; i > 0; i++) { ... }循環將無限期地運行,這意味着最後的System.out.println("Your total points: " +point);行永遠不會到達。您需要修復for循環,因此它只運行有限次數。

在每輪之後的積分從未添加到if部分的總計中,您需要更改語句以便point +=而不是point =

我在下面的代碼中添加了一些註釋,以便您可以查看已做出哪些更改。我也關閉了掃描儀在,因爲它是常見的做法到底,並固定爲清楚起見,代碼縮進:

public class J1 
{ 
    public static void main(String args[]) 
    { 
     // create random object 
     java.util.Random rand = new java.util.Random(); 
     java.util.Scanner scanner = new java.util.Scanner(System.in); 

     // check next int value 
     System.out.println("Wall height: " + rand.nextInt(10)); 
     double height = rand.nextInt(10); 

     System.out.println("Distance from wall: " + rand.nextInt(20)); 
     double dist = rand.nextInt(20); 

     double point = 0; //ADD THIS LINE 
     for(int i = 1; i < 10; i++) //CHANGED SO THAT i < 10 INSTEAD OF i > 0 
     { 
      System.out.println("Set a lanuch angle between 0 and 90: "); 
      double angle = scanner.nextDouble(); 
      System.out.println("Set a lanuch speed: "); 
      double speed = scanner.nextDouble(); 

      //double point; *** REMOVE THIS LINE *** 
      double a; 
      double b; 
      double c; 
      double d; 
      //double e; 
      double y; 
      a = dist*(Math.tan(Math.toRadians(angle))); 
      b = 9.81*(dist*dist); 
      c = (speed * Math.cos(angle)) * (speed * Math.cos(Math.toRadians(angle))); 
      d = 2*c; 
      y = a - (b/d); 
      System.out.println("Your max height is " +y+ " high"); 

      double space; 
      space = height - y; 

      if(space <= 3 && space > 0) 
      { 
       System.out.println("You just made it! "); 
       point += 0 - 1 + 3; //ADDED += INSTEAD OF = 
       System.out.println("You have " +point+ " points."); 
      } 

      if(space > 3 && space <= 6) 
      { 
       System.out.println("Aww. Plenty of room!"); 
       point += 0 - 1 + 5; //ADDED += INSTEAD OF = 
       System.out.println("You have " +point+ " points."); 
      } 

      if(space <= 0 && space >= -3) 
      { 
       System.out.println("So close!"); 
       point += 0 - 1 - 2; //ADDED += INSTEAD OF = 
       System.out.println("You have " +point+ " points."); 
      } 

      if(space < -3) 
      { 
       System.out.println("Terrible aim!"); 
       point += 0 - 1 - 4; //ADDED += INSTEAD OF = 
       System.out.println("You have " +point+ " points."); 
      } 
     } 
     System.out.println("Your total points: " +point); 
     scanner.close(); //ADD THIS LINE 
    } 
}