[英]Printing array result more than once?
I need the println to display only the first result (input[0]) but it is displaying it once for each split. 我需要println仅显示第一个结果(input [0]),但对于每个拆分,它只显示一次。 for example, If I split the string into 5 parts, it will print it once more? 例如,如果我将字符串分成5部分,它将再次打印出来吗?
input: "Chelsea : Arsenal : 2 : 1" 输入:“切尔西:阿森纳:2:1”
output: Chelsea 输出:切尔西
Chelsea 切尔西
Chelsea 切尔西
Chelsea 切尔西
please enter match result: 请输入匹配结果:
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]);
*/
}
}
Move your System.out.println(fruits[0]);
移动您的System.out.println(fruits[0]);
from inside your for loop to outside your for loop like this - 从您的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]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.