Question

."Ask the user to input a number.  You must use an input dialog box for this input....

."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);

}
}

Homework Answers

Answer #1

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);

    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
(8 marks) Write a program to ask user to input an integer and display the special...
Write a program to ask user to input an integer and display the special pattern accordingly. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use loop statements (for, while or do-while). Your program should use only the following 3 output statements, one of EACH of the followings: System.out.print("-"); // print # System.out.print("+"); // print + System.out.println(); // print a newline Your code must work exactly like the following example (the text in bold...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
Write a program that asks the user to type in ages. They will type a negative...
Write a program that asks the user to type in ages. They will type a negative age when they finish entering the ages. The program will print out the average of all of the ages and the oldest age entered. It should end with a newline. Sample output #1 Type a negative for age to exit Enter your age: 21 Enter your age: 22 Enter your age: 21 Enter your age: -8 The average age is: 21.33 The oldest person...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...