在同一行中输入String和int

时间:2015-10-17 12:31:51

标签: java output user-input

如何在同一行输入getch()String?然后我想继续它从我已输入的int中获取最大数字: 这是我到目前为止编写的代码。

int

输入其他姓名和号码时出现错误。

我希望输出会像这样

  

姓名&编号:约翰45   姓名&编号:保罗30
  姓名&号码:安迪25

     

结果:John 45

2 个答案:

答案 0 :(得分:1)

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String InputValue;
    String name[] = new String[6];
    int number[] = new int[6];
    String LargeName = "";
    int largest = 0;
    int as = 5;

    for (int x = 1; x <= as; x++) {
        System.out.print(" Name & number : ");
        InputValue = in.nextLine();
        String[] Value = InputValue.split(" ");
        name[x] = Value[0];
        number[x] = Integer.parseInt(Value[1]);
    }

    for (int x = 1; x < number.length; x++) {
        if (number[x] > largest) {
            largest = number[x];
            LargeName = name[x];
        }
    }
    System.out.println("Result = " + LargeName + " " + largest);
}

希望这适合你。

答案 1 :(得分:0)

System.out.print(" Name & number : ");
/*
    Get the input value "name and age" separated with space " " and splite it.
    1st part is name and second part is the age as tring format!
*/
String[] Value = in.nextLine().split(" ");
name[x] = Value[0];
// Convert age with string format to int.
number[x] = Integer.parseInt(Value[1]);