[英]I am having difficulty for multiple printing in scanner using array and if-else statement
I am having trouble about multiple printing using array, loops, and if-else statement我在使用数组、循环和 if-else 语句进行多次打印时遇到问题
source code:源代码:
import java.util.*;
public class Main{
public static void main(String[]args)
{
Scanner x = new Scanner (System.in);
System.out.print("Enter the number of players: ");
int numplay = x.nextInt();
int players[]= new int[numplay];
int y;
for(int i=0; i < numplay; i++)
{
System.out.print("Goals score by player #"+ (i+1) +": ");
y = x.nextInt();
players[i]=y;
if (players[i]<=10)
{
System.out.println("Okay, fine, it's Messi");
}
else
{
System.out.println("Not Messi");
}
}
}
}
Output:输出:
Enter the number of players: 3
Goals score by player #1: 2
Okay, fine, it's Messi
Goals score by player #2: 3
Okay, fine, it's Messi
Goals score by player #3: 4
Okay, fine, it's Messi
Possible output should be like this可能的输出应该是这样的
Enter the number of players: 3
Goals score by player #1: 2
Goals score by player #2: 3
Goals score by player #3: 4
Okay, fine, it's Messi
Not Messi Situation:不是梅西情况:
Enter the number of players: 3
Goals score by player #1: 10
Okay, fine, it's Messi
Goals score by player #2: 13
Not Messi
Goals score by player #3: 2
Okay, fine, it's Messi
Not Messi Situation Possible Output:不是梅西情况可能的输出:
Enter the number of players: 3
Goals score by player #1: 10
Goals score by player #2: 13
Goals score by player #3: 2
Not Messi
PS I am practicing my array and I am having trouble with it. PS 我正在练习我的阵列,但遇到了问题。
What you need is a boolean
flag which will be set if any of the players has number of goals greater than 10.您需要的是一个
boolean
标志,如果任何球员的进球数大于 10,就会设置该标志。
At the end, if the flag is set, it will output Not Messi
.最后,如果设置了标志,它将输出
Not Messi
。
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner x = new Scanner (System.in);
System.out.print("Enter the number of players: ");
int numplay = x.nextInt();
int players[] = new int[numplay];
boolean exceededBy10 = false;
for(int i = 0; i < numplay; i++)
{
System.out.print("Goals score by player #" + (i + 1) + ": ");
players[i] = x.nextInt();
if (players[i] > 10)
{
exceededBy10 = true;
}
}
if (exceededBy10) {
System.out.println("Not Messi");
}
else {
System.out.println("Okay, fine, it's Messi");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.