【发布时间】:2015-12-28 20:30:14
【问题描述】:
我必须设计一个程序,在表单中获得 3 个用户输入:
Name1 (any number of spaces) age1
Name2 (any number of spaces) age3
Name3 (any number of spaces) age3
然后打印年龄最大的行(假设 Name3 age3 的年龄最大,我会打印他的行)。
我的代码:
import java.util.*;
public class RankAge{
public static void main(String[] args){
System.out.println("Enter 3 different names and the age of each person respectively:");
Scanner sc = new Scanner(System.in);
String n1 = sc.nextLine();
String n2 = sc.nextLine();
String n3 = sc.nextLine();
}
}
我知道如何扫描用户输入,但我不知道如何比较获取的字符串中的数字以打印特定的数字(也因为可以有任意数量的空格,所以看起来更多对我来说很复杂)。
【问题讨论】:
-
您可以使用
n1.split("\\s+")将字符串拆分为任意数量的空格。
标签: java string input integer compare