#Quick question
8 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @fathom tree! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Please format your code to make it more readable.
For java, it should look like this:
````java
public void foo() {
}
````
when i type -1 i want the output to be " Score must be between 0 and 10 Enter another score or 99 to quit" not this "Score must be between 0 and 10
Enter a score "
This is the code
import java.util.ArrayList;
import java.util.Scanner;
public class CommaPlacement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> myList = new ArrayList<String>();
int validScore = 0;
int invalidScore = 0;
int highestScore = Integer.MIN_VALUE;
int lowestScore = Integer.MAX_VALUE;
int totalScore = 0;
String input;
do {
if (validScore == 0) {
System.out.println("Enter a score ");
} else {
System.out.println("Enter another score or enter 99 to quit ");
}
input = sc.nextLine();
if (!input.equals("99")) {
try {
int score = Integer.parseInt(input);
if (score < 0 || score > 10) {
System.out.println("Score must be between 0 and 10");
invalidScore++;
} else {
myList.add(input);
validScore++;
totalScore += score;
highestScore = Math.max(highestScore, score);
lowestScore = Math.min(lowestScore, score);
}
} catch (NumberFormatException e) { //remove this
System.out.println("Invalid input. Please enter a valid score.");
}
}
} while (!input.equals("99"));
}
}
Why do you have that if-else contraption at the top of you while loop ?
Why not simply print Enter another score or 99 to quit if the score is not in bounds ?
Enter a score >> -1
Score must be between 0 and 10 Enter another score or 99 to quit >> 11
Score must be between 0 and 10 Enter another score or 99 to quit >> 8
Enter another score or 99 to quit >> 3
Enter another score or 99 to quit >> 5
Enter another score or 99 to quit >> 0
Enter another score or 99 to quit >> 10
Enter another score or 99 to quit >> 11
Score must be between 0 and 10 Enter another score or 99 to quit >> 9
Enter another score or 99 to quit >> 99
6 valid scores were entered
3 invalid scores were entered
Highest score was 11
Lowest score was -1
Quiz Score Average was 5.833333333333333
This is what i want to do
so what do you mean @livid marlin ?
From my understanding Enter a score should only appear once for the first iteration.
Why keep invalidScore , validScore and totalScore. invalidScore is totalScore - validScore and in your first if you should check whether totalScore is greater than 0 rather than checking if valid score is greater than zero.