Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 水平获取键盘输入?_Java_String_Input - Fatal编程技术网

Java 水平获取键盘输入?

Java 水平获取键盘输入?,java,string,input,Java,String,Input,下面是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(我需要水平输入,而不是垂直输入) 那么我需要做什么改变呢?像任何函数一

下面是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()
还是如何使用

你能告诉我我的程序出了什么问题吗

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!");
   }
}

使用split函数

 String s = scanner.nextLine();
 String[] inputs = s.split(" ");
如果输入为John 22 500,则输入将等于[“John”、“22”、“500”]

您可以通过执行以下操作将项目1和2转换为整数

int age=Integer.valueOf(inputs[1]) 

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