2017-05-28 333 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 
    } 
}