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
答案 0 :(得分:0)
你永远不会真正存储sum of inputs
,分数也会最大化为4,因为for循环将运行4次。最后看看你可能需要的代码:
import java.util.Scanner;
class Example {
// Keep scanner reference here
Scanner scanner = new Scanner(System.in);
/**
* Constructor
*/
public Example() {
runnerScore();
}
/**
* Main method
*
* @param param
*/
public static void main(String[] param) {
new Example();
}
/**
* A method
*/
public void runnerScore() {
int score = 1;
int sumOfInputs = 0;
// for loop for the questions
for (int i = 0; i <= 3; i++) {
int input = input(score); // You need to save somewhere the input
// you get from the user....
System.out.println("The (" + i + ") time , input from user was " + input);
sumOfInputs += input; // sum every input you get
++score; // which is like using score = score + 1 or score+=1
}
// boolean expression and have used if-else statement
if (sumOfInputs <= 32) {
// prints out what the score is and if it is legal
System.out.println("\nThe team has " + sumOfInputs + " points so is legal");
} else if (sumOfInputs > 32) {
// prints out if the score is not legal
System.out.println("\nThe team has " + sumOfInputs + " points so is NOT legal");
}
}
/**
* Return the input from the user
*
* @param score
* @return
*/
public int input(int score) {
int scannerInput = 0;
System.out.println("\nWhat is the disability class of runner " + score + "?");
// String -> integer
scannerInput = Integer.parseInt(scanner.nextLine());
// Return it
return scannerInput;
}
}