1.How do I add the score when the user inputs the number. I don't understand. It keeps on printing out 4 instead of a higher score. Also, I need to return a boolean answer and when the 4 scores are more than 32 then the else if statement gets printed.
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
You never actually store the sum of inputs
also the score will maximize at 4 cause the for loop will run 4 times . Finally have a look at the code you may need:
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.