Question

In: Computer Science

I am trying to write a simple game in Java named GuessNumber.java to run on the...

I am trying to write a simple game in Java named GuessNumber.java to run on the command line. This program asks user to pick a number and the computer guesses the number repetitively until the guess is correct and will allow the user to set the lower and upper bound as command line parameters from where the picks the number. If no command line parameters are specified then the program uses a default of 1 to 99.

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.util.Random;

import java.util.Scanner;

public class GuessNumber {

    public static void main(String[] args) {

        int upper, lower;

        if (args.length == 2) {

            lower = Integer.parseInt(args[0]);

            upper = Integer.parseInt(args[1]);

        } else {

            lower = 1;

            upper = 99;

        }

        Scanner scan = new Scanner(System.in);

        Random rand = new Random();

        int random;

        int guess;

        random = rand.nextInt(upper - lower) + lower;

        System.out.print("Please guess a number between " + lower + " to " + upper + " (inclusive): ");

        guess = scan.nextInt();

        while (guess != random) {

            System.out.println("Your guess wrong! Try again!");

            System.out.print("Please guess a number between " + lower + " to " + upper + " (inclusive): ");

            guess = scan.nextInt();

        }

        System.out.println("Congratulations! You got it!");

    }

}


Related Solutions

I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
I am trying to write a program in Java: Pick 4 cards and display the cards...
I am trying to write a program in Java: Pick 4 cards and display the cards and their total value (Ace = 1, King = 13, Queen = 12, and Jack 11...etc. , BUT you should check that no card is a duplicate... there is only one "Ace of Spades" per deck for example. I need to utilize the displayMenu so the program continues to run for the user without breaking. My program is not running correctly. import java.util.Scanner; import...
I am working java project. I need to write methods for text game. I'm working on...
I am working java project. I need to write methods for text game. I'm working on chest class. Description : You should start your work by implementing the Chest class (a skeleton is provided for you). It needs to store whether it is locked or not, a String describing the contents, and which key it was locked with. The method stubs for this class are provided with comments, be sure to implement them the way they are described. You should...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
I am trying to run a regression in R with a continuous dependent variable and a...
I am trying to run a regression in R with a continuous dependent variable and a binary independent variable? The binary one refers to whether a person received treatment or not. how do I interpret the regression outputs since I dont get results based 0 or 1 buf the whole variable?
I need to set up and run a goodness of fit test. I am trying to...
I need to set up and run a goodness of fit test. I am trying to learn more about the type of novels that people prefer to read. Please include null and alternative hypotheses. Show all work and please explain if you use any excel tests. Total number of responses is 30. Romance: 18 Sci - Fi: 7 Fiction: 5
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
Is it possible to run Levene's test with multiple comparisons in minitab? I am trying to...
Is it possible to run Levene's test with multiple comparisons in minitab? I am trying to run equal variances, the levene's test, and minitab somehow doesn't like the data. I already made 3 columns in minitab sheet prompt: A Datamore Company designs applications software for a number of large firms. A study is conducted to assess the satisfaction of these firms with the supplied software. Datamore decides to determine if the type of industry [Human Resources, Information Technology, Financial, and...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions.. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT