2016-07-07 116 views
-2

不知道为什么我的代码被无限循环捕获..也许我写的代码错了?或缩进? 输出没有给出任何错误它陷入了无限循环

public class HelloWorld 
{ 
    public static void main(String[] args) 
    { 
    //Not sure if it's a better way but I did this way: 
final int TRIES = 5; 
int heads = 0; 
for(int i=0; i<=TRIES; i++) 
{ 
    Scanner input = new Scanner(System.in); 
    System.out.print("Do you wanna continue? Y or N? "); 
    char c = input.next().charAt(0); 
    c = Character.toUpperCase(c); 
    if(c =='y'){ 
    int r = (int) Math.random * 2) + 1; //A number between 1 & 2 

     if(r == 1) 
     { 
     System.out.print("heads"); 
     heads++; 
     } 
     else{ 
     System.out.print("Lost"); 
     } 
    } 

    else{ 
     System.out.print(heads); 
    } 
    } 
} 

}

+2

此代码不能编译。 –

+0

*“or indentation”*这只会导致StackOverflow用户出现问题或代码可读性问题,而不是JVM本身。 – Tom

+0

这里没有无限循环。你将循环6次并完成(在代码被固定到足以编译之后)。你没有做任何事情来影响循环索引。什么让你觉得你有一个无限循环? – azurefrog

回答

1

我也不清楚,但我认为这个代码应该很好地工作:

import java.util.Scanner; 
import java.util.Random; 
public class HelloWorld { 
    public static void main(String[] args) { 
     //Not sure if it's a better way but I did this way: 
     final int TRIES = 5; 
     int heads = 0; 
     for(int i=0; i<=TRIES; i++) { 
      Scanner input = new Scanner(System.in); 
      System.out.print("Do you wanna continue? Y or N? "); 
      char c = input.next().charAt(0); 
      c = Character.toUpperCase(c); 
      if(c =='Y') { 
       int r = (int) (Math.random() * 2) + 1; //A number between 1 & 2 
       if(r == 1) { 
        System.out.println("heads"); 
        heads++; 
       } else { 
        System.out.println("Lost"); 
       } 
      } else { 
       System.out.print(heads); 
      } 
     } 
    } 
} 
+0

它没有。回答'N'仍然会问“你想继续吗?”反复。 ---另外,你没有解释你改变了什么以及你为什么改变了。总之,答案并不是那么有用。该代码中至少存在两个问题。 – Andreas

+0

非常感谢..它确实编译..我唯一的错误是一个额外的cosing大括号..你帮我了很多..我也投票了你up –

1
public class HelloWorld 
{ 
    public static void main(String[] args) 
    { 
     //Not sure if it's a better way but I did this way: 
     final int TRIES = 5; 
     int heads = 0; 
     for(int i=0; i<=TRIES; i++) 
     { 
      Scanner input = new Scanner(System.in); 
      System.out.print("Do you wanna continue? Y or N? "); 
      char c = input.next().charAt(0); 
      c = Character.toUpperCase(c); 
      if(c =='Y'){ 
       int r = (int) (Math.random() * 2) + 1; //A number between 1 & 2 

       if(r == 1) 
       { 
        System.out.println("heads"); 
        heads++; 
       } 
       else{ 
        System.out.println("Lost"); 
       } 
      } 

      else{ 
       System.out.println(heads); 
       break; 
      } 
     } 
    } 

} 

我不知道是什么你试图实现,但是这个代码编译,而且当你说你不想继续时,它会停止循环。使用休息不是最好的做法,但它有效。它仍然只会要求最多5次,因为这是您放入的限制(由TRIES变量给出)。如果你想运行一个无限循环停止,当用户不希望继续下去,你migth想用一个布尔变量,一段时间,像这样:你正变成输入字符

boolean stop = false; 
    while(!stop) 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Do you wanna continue? Y or N? "); 
     char c = input.next().charAt(0); 
     c = Character.toUpperCase(c); 
     if(c =='Y'){ 
      int r = (int) (Math.random() * 2) + 1; //A number between 1 & 2 

      if(r == 1) 
      { 
       System.out.println("heads"); 
       heads++; 
      } 
      else{ 
       System.out.println("Lost"); 
      } 
     } 

     else{ 
      System.out.println(heads); 
      stop=true; 
     } 

注大写,然后将它压缩到'y',这将永远是错误的。

+0

我喜欢它,你用两种方法解决这个问题..非常感谢!不幸的是我只能选择一个答案.. –