User Input with a Do Loop
Alright, so I'm trying to code a program that will have the user input a collection of names and scores and allow them to calculate the highest and second highest scores. The two main questions I have are as follows:
CODE:
package homework7;
import java.util.Scanner;
public class ScoreCalculator {
public static void main(String[] args) {
int i = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the number of students: ");
int count = keyboard.nextInt();
keyboard.nextLine();
do {
i++;
System.out.print("Please enter score number " + i + ": ");
int score = keyboard.nextInt();
keyboard.nextLine();
} while (i <= count);
}
}
- How can I change it to "Please enter name and score number" in the do loop and still extract only the int value despite the fact that a String is also being entered?
- How can I store each int the user inputs into a separate variable within the do loop, and then extract those values and use them at the end to calculate the two highest scores?
Please keep it simple - this is for a class and we aren't very in depth yet. That means no arrays, no separate classes, no files or lists.
Re: User Input with a Do Loop
To see how to use the Scanner class, write a small simple test program and try using different combinations of the next... methods and by entering different combinations of data before pressing Enter.
Quote:
How can I store each int the user inputs into a separate variable
The best way would be to use an array and change the index to the array every time you store something in it.
Re: User Input with a Do Loop
Quote:
Originally Posted by
RadiantChaos
Please keep it simple - this is for a class and we aren't very in depth yet. That means no arrays, no separate classes, no files or lists.
Quote:
Originally Posted by
Norm
The best way would be to use an array and change the index to the array every time you store something in it.
As I said, we haven't covered arrays yet, so I need a solution that is more complicated, but not as in depth.
Also, not really sure what you were suggesting at first - care to elaborate, please?
Re: User Input with a Do Loop
Quote:
Originally Posted by
RadiantChaos
Also, not really sure what you were suggesting at first - care to elaborate, please?
Scratch that, I figured that part out. Still need help on the storing of multiple scores, though.
Re: User Input with a Do Loop
Do you only need to save the two highest scores and none of the others? Then you will only need two variables to save those values in.