."Ask the user to input a number. You must use an input dialog box for this input. Be sure to convert the String from the dialog box into an integer (int). The program needs to keep track of the smallest number the user entered as well as the largest number entered. Use a Confirm dialog box to ask the user if they want to enter another number. If yes, repeat the process. If no, output the smallest and largest number that the user entered. This program only outputs the largest and smallest number once AT THE END of the program when the user wants to quit. Also, your program should account for the case when the user only enters one number. In that case, the smallest and largest number will be the same."
Now, here is the issue. The program asks what number, I type in 9. It asks do i want to type in another number y/n. I hit no, and the program ends successfully and uses the same for max and min. BUT, when I type in Y, it does the same thing as N does. When I hit Y, it is supposed to repeat the program until the user asks for it to end. At the end, out of all the numbers the user entered, it is supposed to put the biggest number and the smallest. For example the user chose numbers 1. 7. 8 10
The end should say
max: 10
min; 1
HERE IS MY CODE.
package additiongame;
import java.util.Random;
import java.util.Scanner;
public class AdditionGame
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Random r = new Random(); // random number generator
int numberOfGame, correct = 0, incorrect = 0, sum, userSum;
System.out.print("How many attempt do you want: ");
numberOfGame = sc.nextInt();
for (int i = 1; i <= numberOfGame; i++)
{
int random1 = r.nextInt(101); // range is [0 100]
int random2 = r.nextInt(101);
System.out.print("Please try. " + random1 + "+" + random2 + ":
");
userSum = sc.nextInt(); // user answer
if (userSum == (random1 + random2))
{
System.out.println("Correct");
correct++;
} else
{
System.out.println("Wrong!!. Correct answer is: " + (random1 +
random2));
incorrect++;
}
}
System.out.println("Correct attempt: " + correct + ", Wrong attempt: " + incorrect);
}
}
thanks for the question, here is the complete code in Java, I have given plenty of comments so that you can follow each line of the code.
Also, I ran your given inputs and printed the min and max value correctly. If you read through the comments you can follow the same steps for similar kind of problems in future.
Thank you, let me know in case you have any questions.
============================================================
import java.util.Scanner;
public class MinMax {
public static void
main(String[] args) {
Scanner scanner =
new Scanner(System.in);
// delcare two variable
min and max
int
minNumber = 0, maxNumber = 0;
boolean
firstTime = true;
String choice =
"";
// keep asking for a
number until user enter N and wants to quit
while
(true) {
System.out.print("Enter your number:
");
int userNumber = scanner.nextInt();
scanner.nextLine(); // for consuming the new line charaacter
// when the firstTime is true we
assign the first number
// to both min and max and set the firstime vlaue to False
if (firstTime) minNumber = maxNumber =
userNumber;
// we check if the number entered is less than min, if true we set
the
// new min value to the current value
if (userNumber < minNumber) minNumber =
userNumber;
// we check if the number entered is greater than min, if true we
set the
// new max value to the current value
if (userNumber > maxNumber) maxNumber =
userNumber;
firstTime = false; // once we set the first value
to both min and max we set this value to false
// enter a loop until user enters either Y or N only
while
(true) {
System.out.print("Do you want to enter
again (Y for yes N for No): ");
choice = scanner.nextLine().toUpperCase();
if (choice.equals("Y"))
break;
else if (choice.equals("N"))
break;
else
System.out.println("Please enter either Y
or N only. Try again.");
}
if (choice.equals("N"))
break;
}
// print both min and
max values at the end
System.out.println("Min:
"+minNumber);
System.out.println("Max:
"+maxNumber);
}
}
Get Answers For Free
Most questions answered within 1 hours.