Reputation: 53
I need to write a program that finds the best time to meet using 4 inputs from 4 users. Each user will enter 1, 2, or 3 depending on the best time for them. 1 is 8:00-12:00, 2 is 12:00-18:00, and 3 is 18:00-23:00. At the end of the program, it will display the time that had the most inputs, or if two times had an equal amount of inputs, the earliest one. Here I'm not sure what to use really, at first I thought of using a very long series of if statements but that really doesn't work out here. I'm leaning toward using while statements but I don't know how to effectively use them and then take an input from the user and depending on that input (if it's 1, 2, or 3) adding it to a counter which will then be used to see which time had the most hits. Any help would be appreciated! Should I be using one variable for all of the inputs as well?
Scanner kbd=new Scanner(System.in);
System.out.print("Meeting Times: \n"+"1) 8:00-12:00 2) 12:00-18:00 3) 18:00-23:00\n");
System.out.print("\nChoose options 1, 2, or 3.\n");
int p, morning=0, noon=0, evening=0;
String first = "8:00-12:00", second="12:00-18:00",third="18:00-23:00";
while (p>4){
System.out.print("Enter the best time available for person 1.");
p=kbd.nextInt();
morning++;
}kbd.close();
Upvotes: 0
Views: 1202
Reputation: 711
What's the logic behind p > 4
?. If you know for sure that there will be 4 persons that you need input from, you could use a simple for
loop.
for (int i = 0; i < 4; i++) {
System.out.print("Enter the best time available for person " + (i+1) + ".");
p=kbd.nextInt();
if (p == 1) morning++;
else if (p == 2) noon++;
else if (p == 3) evening++;
}
Note that you could also simply prompt the user to enter the number of persons, take it as an int and replace the loop condition accordingly.
To get the maximum value we can use Math.max
, however, it only takes two input values so we'll have to call it twice:
int max = Math.max(morning, Math.max(noon, evening));
Then we can ultimately give a response to the user;
if (max == morning) System.out.println("Morning is the way to go!");
else if (max == noon) System.out.println("Noon is the way to go!");
else System.out.println("Evening is the way to go!");
If you want, you could add on to the functionality, for example by requesting new input if the given input is not valid (e.g. a String
or a number not in the range), but perhaps that's out of your scope.
Upvotes: 1