2014-08-27 68 views
-1

以下是Java程序的預期輸出。水平獲取鍵盤輸入?

Hello. What is your name? 
Hi John! How old are you? 
So you're 22 eh? That's not old at all! 
How much do you make John? 


500.0! I hope that's per hour and not per year! LOL! 

預期輸入: John 22 500(我需要水平地得到輸入,而不是垂直地)。

那麼我需要做些什麼改變?像任何功能scanner.next()而不是nextLine() shoudl被使用或如何?

你能告訴我的程序有什麼問題嗎?

public class Test2 { 
    public static void main(String args[]) { 

     Scanner scanner = new Scanner(System.in); 
     Scanner in = new Scanner(System.in); 
     Scanner inc = new Scanner(System.in); 

     String xyz = scanner.nextLine(); 
     int age = in.nextInt(); 
     double cme = inc.nextInt(); 
     System.out.println("Hello.What is your name?"); 
     System.out.println("So you're "+age+"eh?That's not old at all!"); 
     System.out.println("hi"+xyz+"!How old are you?"); 
     System.out.println("How much do you make"+xyz+"?"); 
     System.out.println(+cme+"!I hope that's per hour and not per year!LOL!"); 
    } 
} 

回答

0

使用分割功能。

String s = scanner.nextLine(); 
String[] inputs = s.split(" "); 

如果輸入是約翰22 500個輸入將等於[ 「約翰」, 「22」, 「500」]

可以通過執行

轉換項目1和2,以整數
int age=Integer.valueOf(inputs[1]) 

int pay = Integer.valueOf(inputs[2])