2013-06-21 55 views
0

当我键入存储在字符串变量bp内部的内容时,它不响应,直到我多次单击输入键。当使用扫描仪时输入输入时延迟

在此之后,程序只打印出else语句中的内容。 程序响应的唯一字符串变量是j

代码:

package legit; 

import java.util.Scanner; 

public class Gamee { 


    public static void main(String args[]){ 

     Scanner sc = new Scanner(System.in); 
     String j = "good, how are you?"; 
     String b = "good"; 
     String p = "bad"; 

     System.out.println("Hello, my name is Julie the Robot"); 

     System.out.println("How Are You?"); 


     if (j.equals(sc.nextLine())) { 
     System.out.println("Im Doing Great!"); 


     }else if (b.equals(sc.nextLine())) {     
     System.out.println("Thats Great! :)"); 


     }else if (p.equals(sc.nextLine())){    
     System.out.println("Thats not good"); 

     }else { 
     System.out.println("I see..."); 
     } 
+1

每个'sc.nextLine()'从控制台读取一个新输入。我想你现在可以猜测出了什么问题。 –

回答

1

你知道吗,每次使用时间sc.nextLine()你问用户新的输入?

尝试在您的if之前使用一次,并将接收到的输入存储为值,然后在条件中使用该值。