2012-02-25 39 views
1

我对Java编程极其新颖,并且我确信这是一个非常简单的修复。我有下面的代码,它的工作原理,但我想知道如何改变它,所以如果用户输入的东西不是一个int,它会循环回到顶部后,它会给出错误消息,所以它会要求再次温度。提前致谢。如何返回代码顶部?也许?

Scanner in = new Scanner(System.in); 
    //User input of temperature 
    System.out.print("Enter temperature in Celsius: "); 
    if (in.hasNextInt()) 
    { 
     int temperature = in.nextInt(); 
     //Now determine what state the water will be in, either ice, gas, or water. 
     if (temperature >=100) 
     { 
      System.out.print("Gas!"); 
     } 
     else if ((temperature <100) && (temperature >0)) 
     { 
      System.out.print("Water!"); 
     } 
     else if (temperature <=0) 
     { 
      System.out.print("Ice!"); 
     } 
    } 
    else 
    { 
     System.out.println("Error: Not an Integer"); 
     System.out.print("Please enter temperature in Celsius: "); 
    } 

回答

5

招入的方法

public static void main(String args[]){ 

    Scanner in = new Scanner(System.in); 
    readInput(in); 
} 

public static void readInput(Scanner in){ 

    System.out.print("Enter temperature in Celsius: "); 
    if (in.hasNextInt()){ 
     // do your stuff here 
    } 
    else { 
     // print the errors 
     readInput(in); 
    } 
} 
+2

不错的使用递归在这里 – kaveman 2012-02-25 23:50:29

+0

你的方法肯定比我的更复杂。 +1。 – simchona 2012-02-25 23:53:42

+0

@simchona,它们是等价的,但这种方式可能更具可读性;但是可以(并且实际上应该)将while循环放入新方法 – scibuff 2012-02-25 23:56:10

1

它看起来像你应该考虑使用while()循环。您可以使用一个片段like this

Scanner scanner = new Scanner(System.in); 
int input = -1; 
input = scanner.nextInt(); 
while (input != 0) 
{ 
System.out.println("Enter 0 to exit the loop."); 
input = scanner.nextInt(); //without this, you will hit a loop that never ends. 
{ 

,您可以添加你的代码将温度转换这个循环中。

+1

这是一个很好的例子,其中'do ... while'是合适的,所以你不需要编写'input = scanner.nextInt();'两次 – scibuff 2012-02-25 23:49:44

0

答案最简单的解决方案是将代码粘贴到无限的“运行”循环中。

Scanner in = new Scanner(System.in); 
while (true) { 
    //User input of temperature 
    System.out.print("Enter temperature in Celsius: "); 
    if (in.hasNextInt()) 
    { 
     int temperature = in.nextInt(); 
     //Now determine what state the water will be in, either ice, gas, or water. 
     if (temperature >=100) 
     { 
      System.out.print("Gas!"); 
     } 
     else if ((temperature <100) && (temperature >0)) 
     { 
      System.out.print("Water!"); 
     } 
     else if (temperature <=0) 
     { 
      System.out.print("Ice!"); 
     } 
    } 
    else 
    { 
     System.out.println("Error: Not an Integer"); 
     System.out.print("Please enter temperature in Celsius: "); 
    } 
} 

Run loops,或主回路为他们通常被称为,提供一致的循环,接受用户输入并提供反馈。如果/当你进入多线程/ GUI编程时,这是你的大部分UI代码去的地方。然而,大多数基于项目的环境(适用于Android应用的Eclipse,适用于iOS应用的Xcode等)具有比while (true)更复杂和惯用的运行循环。

+0

应该可以离开'Scanner in = new Scanner(System。in);'退出循环 – scibuff 2012-02-25 23:57:03

+0

呐喊;好决定。会做。 – 2012-02-25 23:57:33

+0

我实际上是用这种方法去的,因为当我尝试第一个时,我得到了一百万个错误(我可能做了一些错误的事情,正如我所说的,我是非常新的)。我加了一个休息时间;在所有的if/else if之后,它在那里工作的很好,但是如果我输入字母例如并且它转到else语句,它会停止循环,并显示“输入摄氏温度:错误:不是整数请输入温度摄氏度:“。所以我想现在我正在试图弄清楚如何将它从其他回到顶端,并在那里停下来等待另一个用户输入 – DJ87 2012-02-26 03:12:43