2017-05-28 14 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 +=になるようにステートメントを変更する必要があります。あなたは変更が行われているかを見ることができるように

は、私は以下のコードでいくつかのコメントが追加されました。また、私はそれが一般的であるとして、最後にスキャナを閉じ、かつ明瞭にするためにコードのインデントを修正した:

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 
    } 
} 
関連する問題