我需要println只显示第一个结果(input [0]),但是每次分割都会显示一次。例如,如果我将字符串分成5个部分,它会再次打印出来吗?
输入:"切尔西:阿森纳:2:1"
输出: 切尔西
切尔西
切尔西
切尔西
请输入匹配结果:
Scanner sc = new Scanner(System.in);
for (int b=0; b < 5; b++){
System.out.println("please enter match result:");
String s = sc.nextLine();
String input[] = s.split(":"); // parse strings in between the dash character
for(String temp : input ) {
String hometeam = input[0];
String awayteam = input[1];
String homescore = input[2];
String awayscore = input[3];
System.out.println(input[0]);
/*for(int a=0; a<input.length; a++)//length is the property of array
System.out.println(input[a]);
*/
}
}
答案 0 :(得分:0)
将您的System.out.println(fruits[0]);
从for循环内移到for循环之外,如下所示 -
Scanner sc = new Scanner(System.in);
for (int b=0; b < 5; b++) {
System.out.println("please enter match result:");
String s = sc.nextLine();
String input[] = s.split(":"); // parse strings in between the dash character
for(String temp : input ) {
String hometeam = input[0];
String awayteam = input[1];
String homescore = input[2];
String awayscore = input[3];
}
System.out.println(input[0]);
}