【发布时间】:2016-12-01 20:36:44
【问题描述】:
1.如何在用户输入数字时添加分数。我不明白。它继续打印出 4 而不是更高的分数。另外,我需要返回一个布尔值,当 4 个分数超过 32 时,打印 else if 语句。
import java.util.Scanner;
class forloops
{
public static void main (String[] param)
{
runnerscore();
System.exit(0);
} //END main
public static void runnerscore()
{
int score = 1; // initialising the score that is kept throughout the program
int input = 0;
for (int i = 0; i<=3; i++) // for loop for the questions
{
input(score, input); // 2nd method
score = score + 1;
}
if (score<=32) // boolean expression and have used if-else statement
{
System.out.println("The team has " + score + " points so is legal"); // prints out what the score is and if it is legal
}
else if (score >32)
{
System.out.println("The team has " + score + " points so is NOT legal"); // prints out if the score is not legal
}
}
public static int input(int score, int input)
{
Scanner scanner = new Scanner(System.in); //enables scanner that lets the user input
System.out.println("What is the disability class of runner " + score + "?");
input = Integer.parseInt(scanner.nextLine());
return input;
}
}//END DisabilityRate
【问题讨论】:
-
你认为
input(score, input);是做什么的? -
使用 for 循环打印出方法 input() 中的内容?并将 int 分数、int 输入作为参数传递给第二种方法...
-
您将
score增加了 4 次。为什么你认为它会超过 4 个? -
我认为我的问题不够清楚,在这里很难解释我的意思。我想要做的是,当问题'System.out.println(“跑步者的残疾等级是什么”+得分+“?”)时添加用户输入的任何内容;被问到。如果分数是 32,那么它打印出 else if 语句。
-
嗯,用简单的英语,你想运行 for 循环 4 次。如果4个输入的总和> = 32,首先输入if,否则输入第二个if?分数永远不会超过 4...