在 java 中打印特定的用户输入
Printing specific user input in java
我必须设计一个程序来获取 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();
}
}
我知道如何扫描用户输入,但我不知道如何比较获取的字符串中的数字以打印特定的数字(而且因为看起来可以有任意数量的空格对我来说更复杂)。
您可以使用 split
获取此人的年龄:
String age = "Jon 57".split("\s+")[1]; // contains "57"
然后您可以使用 Integer.parseInt(age)
获取此人的年龄,作为数字。
如果您需要允许用户输入带空格的名称,您可以调整方括号中的数字([]
)。例如,[2]
将要求用户输入名字和姓氏。
+1 因为乍一看它似乎很简单,但当我开始实施时,复杂性开始显现。这是您问题的完整解决方案,
import java.util.*;
public class RankAge {
public static void main(String s[]){
System.out.println("Enter 3 different names and the age of each person respectively:");
Scanner sc = new Scanner(System.in);
String n[] = new String[3];
for(int i=0;i<3;i++){
n[i] = sc.nextLine();
}
int age[] = new int[3];
age[0] = Integer.parseInt(n[0].split("\s+")[1]);
age[1] = Integer.parseInt(n[1].split("\s+")[1]);
age[2] = Integer.parseInt(n[2].split("\s+")[1]);
int ageTemp[] = age;
for(int i=0;i<age.length;i++){
for(int j=i+1;j<age.length;j++){
int tempAge = 0;
String tempN = "";
if(age[i]<ageTemp[j]){
tempAge = age[i];
tempN = n[i];
age[i] = age[j];
n[i] = n[j];
age[j] = tempAge;
n[j] = tempN;
}
}
}
for(int i=0;i<3;i++){
System.out.println(n[i]);
}
}
}
在 frenchDolphin 的建议下设法做到了。这是我使用的代码(非常适合初学者):
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 a1 = n1.split("\s+")[1];
String n2 = sc.nextLine();
String a2 = n2.split("\s+")[1];
String n3 = sc.nextLine();
String a3 = n3.split("\s+")[1];
if(Integer.parseInt(a1) > Integer.parseInt(a2)){
} if(Integer.parseInt(a1) > Integer.parseInt(a3)){
System.out.println(n1);
}else if(Integer.parseInt(a2) > Integer.parseInt(a3)){
System.out.println(n2);
}else{
System.out.println(n3);
}
}
}
我必须设计一个程序来获取 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();
}
}
我知道如何扫描用户输入,但我不知道如何比较获取的字符串中的数字以打印特定的数字(而且因为看起来可以有任意数量的空格对我来说更复杂)。
您可以使用 split
获取此人的年龄:
String age = "Jon 57".split("\s+")[1]; // contains "57"
然后您可以使用 Integer.parseInt(age)
获取此人的年龄,作为数字。
如果您需要允许用户输入带空格的名称,您可以调整方括号中的数字(
[]
)。例如,[2]
将要求用户输入名字和姓氏。
+1 因为乍一看它似乎很简单,但当我开始实施时,复杂性开始显现。这是您问题的完整解决方案,
import java.util.*;
public class RankAge {
public static void main(String s[]){
System.out.println("Enter 3 different names and the age of each person respectively:");
Scanner sc = new Scanner(System.in);
String n[] = new String[3];
for(int i=0;i<3;i++){
n[i] = sc.nextLine();
}
int age[] = new int[3];
age[0] = Integer.parseInt(n[0].split("\s+")[1]);
age[1] = Integer.parseInt(n[1].split("\s+")[1]);
age[2] = Integer.parseInt(n[2].split("\s+")[1]);
int ageTemp[] = age;
for(int i=0;i<age.length;i++){
for(int j=i+1;j<age.length;j++){
int tempAge = 0;
String tempN = "";
if(age[i]<ageTemp[j]){
tempAge = age[i];
tempN = n[i];
age[i] = age[j];
n[i] = n[j];
age[j] = tempAge;
n[j] = tempN;
}
}
}
for(int i=0;i<3;i++){
System.out.println(n[i]);
}
}
}
在 frenchDolphin 的建议下设法做到了。这是我使用的代码(非常适合初学者):
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 a1 = n1.split("\s+")[1];
String n2 = sc.nextLine();
String a2 = n2.split("\s+")[1];
String n3 = sc.nextLine();
String a3 = n3.split("\s+")[1];
if(Integer.parseInt(a1) > Integer.parseInt(a2)){
} if(Integer.parseInt(a1) > Integer.parseInt(a3)){
System.out.println(n1);
}else if(Integer.parseInt(a2) > Integer.parseInt(a3)){
System.out.println(n2);
}else{
System.out.println(n3);
}
}
}