2014-10-26 154 views
-2

在eclipse的控制台中,它允许您键入输入以测试程序是否正常工作。输入一个输入后,它不允许输入另一个输入,你如何让eclipse允许你输入多个输入?在控制台中输入输入

import java.util.Scanner; 

public class FracCalcc { 
    public static void main (String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Welcome to FracCalc"); 
    System.out.println("Type expressions with fractions, and I will evaluate them."); 

    if (input.next().equals("quit")){ 
     System.out.println("Thanks for running FracCalc!"); 
    }else{ 
     System.out.println("I can only process quit for now"); 
    } 
    } 
} 

回答

1

查看您的代码后,它应该只接受一个输入是正确的。您需要一个循环才能接受多行输入。

这只运行一次,然后程序将终止。

input.next().equals("quit") 

如果你想要接受多行,直到它等于“退出”,然后使用while循环:

boolean isTrue = true; 

    String in = ""; 
    while(isTrue) { 

     in = input.nextLine(); 
     if (in.equals("quit")) { 
      System.out.println("Thanks for running FracCalc!"); 
      isTrue = false; 
     } else { 
      System.out.println("I can only process quit for now"); 

     } 
    } 
+0

我尝试这样做,但是当我尝试键入在输入它不允许我。难道我做错了什么? – 2014-10-26 19:41:37

+0

现在工作,对此感到遗憾。我为while循环设置了错误的值,所以它从未输入它。 – 2014-10-26 19:55:38