Question

In: Computer Science

java beginner level how do i add messages that say win, too high or too low...

java beginner level how do i add messages that say win, too high or too low for a lottery number generator guessing game?

//generate lottery numbers int lottery = (int) (Math.random() * 500);

int guess = 0; boolean correct = false;

// prompt user to enter a guess Scanner input = new Scanner(System.in);

System.out.print ("Enter your lottery pick (three digits): ");

int number = input.nextInt();

// keep asking user until user enters a 3-digit number while (guess < 100 || guess > 500) { // keep asking user until user enters a 3-digit number System.out.print("Enter your lottery pick (three digits): ");

guess = input.nextInt(); if (number == lottery) { correct = true; System.out.println("you win"); }

else if (number < lottery) { System.out.println("too low"); } else if (number > lottery) { System.out.println("too high"); }

// add error message if guess is not 3 digit number System.out.println("Error: You didnt entered a 3-digit number. Please re-enter.");

System.out.println (" the lottery number is " + lottery); } } }

Solutions

Expert Solution

Code Screenshot :

Executable Code:

import java.util.Scanner;
public class Main {
   //Main Program
   public static void main(String args[]) {
       Scanner input = new Scanner(System.in);
       int guess = 0;
       boolean correct = false;
       int min = 100;
       int max=500;
       int lottery = min + (int)(Math.random() * ((max - min) + 1));
       // prompt user to enter a guess Scanner input = new Scanner(System.in);
       System.out.print ("Enter your lottery pick (three digits): ");
       guess = input.nextInt();
       // keep asking user until user enters a 3-digit number
       while (guess < 100 || guess > 500) { // keep asking user until user enters a 3-digit number
       System.out.println("Error: You didnt entered a 3-digit number. Please re-enter.(100-500");
       System.out.print("Enter your lottery pick (three digits): ");
       guess = input.nextInt();
   }
       if (guess == lottery)
       {
           correct = true;
           System.out.println("you win");
       }
       else if (guess < lottery) {
           System.out.println("too low");
       }
       else if (guess > lottery) {
           System.out.println("too high");
       }
       // add error message if guess is not 3 digit number

System.out.println ("The lottery number is " + lottery);
}
}

Sample Output :

Please comment below if you have any queries thanks :)


Related Solutions

JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
If the interest coverage ratio is too high, then the ROE is too low, Agree or...
If the interest coverage ratio is too high, then the ROE is too low, Agree or Not ? What the three factor affect ROE and how higher ROE lead to higher share price ?
What is your opinion of the minimum wage? Is it too high or too low? Explain....
What is your opinion of the minimum wage? Is it too high or too low? Explain. If the current minimum wage is above what the market would ordinarily bear (equilibrium wage in that market), and there is a proposal to raise the wage, explain the economic effects of the proposal on wages, employment, product prices and costs of the products the minimum wage labor produces (a graph may be useful here). How important is the assumption of the proposed wage...
QUESTION 18 To say that a price is "too low for equilibrium" means that: A. producers...
QUESTION 18 To say that a price is "too low for equilibrium" means that: A. producers are making substantial profits B. consumers want to pay a lower price than sellers are currently charging. C. there will occur a surplus of the product. D. the amount demanded exceeds the amount supplied. QUESTION 19 A point on a demand curve indicates: A. a particular price and the corresponding quantity demanded by consumers. B. a combination of two consumer goods which buyers will...
The typical American diet is too high in saturated fat, sodium, and sugar and too low...
The typical American diet is too high in saturated fat, sodium, and sugar and too low in fruits, vegetables, whole grains, calcium, and fiber. Such a diet significantly increases the risk of numerous diseases, including heart disease, diabetes, obesity, hypertension, stroke, osteoporosis, and many cancers (colon, prostate, mouth, throat, lung, stomach). Overall, unhealthy eating habits and inactivity affect quality of life and cause disabilities. What do you think needs to be done in order to reduce the intake of saturated...
How does a high or low  Lymphocytes level in a cbc lab relate to a client with...
How does a high or low  Lymphocytes level in a cbc lab relate to a client with atrial fibrialltion and chronic kidney disease.
How does a high or low Monocytes level related to a patient with chronic kidney diease...
How does a high or low Monocytes level related to a patient with chronic kidney diease and atrial fibrillation?
How does a high or low Eosinophils level related to a patient with chronic kidney disease...
How does a high or low Eosinophils level related to a patient with chronic kidney disease and atrial fibrillation?
How does a high or low granulocytes level relate to a patient that has atrial fibrillation...
How does a high or low granulocytes level relate to a patient that has atrial fibrillation and chronic kidney disease?
Describe the level of financial leverage used by Microsoft (high, medium, or low).Is that level of...
Describe the level of financial leverage used by Microsoft (high, medium, or low).Is that level of leverage an advantage or disadvantage to the company? Explain your answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT