Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: java help

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java help

    Hello, I am a beginner and I need help with a game where you have to find the right price (the price is a random number).


    class game extends Program{
     
        String name;
     
        void intro(){
    	println("$$$$$$$$$$$$$$$$$$");
    	println("The price is right");
    	println("$$$$$$$$$$$$$$$$$$");
    	println();
    	print("Press enter to play");
    	readString();
    	clearScreen();
    	print("Enter your name : ");
    	name = readString();
    	clearScreen();
        }
     
        void testRightPrice(){
            int price = rightprice();
            assertTrue(0 <= price && price <= 200);
        }
     
        int rightprice (int Price, int RightPrice){
     
            while ((Price<RightPrice)||(Price>RightPrice)){
     
                if (Price<RightPrice){
    			    println("it's +");
    		    }
                else {
    			    println("it's -");
    		    }
            } return
        }
     
     
        int randNum(){
    	return (int) (0+random()*((200-0)+1));
        }
     
     
        /*int lireEntierSecurise(){
    	print(name + " : ");
    	String reponse = readString();
    	if (estNombreEntier(reponse)){
    	    return stringToInt(reponse);
    	}else{
    	    println("It's not a number "+ name +", try again");
    	    return lireEntierSecurise();
    	}
        }
     
        boolean estNombreEntier(String chaine){
    	boolean valide = true;*/
     
     
     
        void algorithm(){
            intro();
            println("You have to guess the price");
            println(rightprice());
        }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    I need help
    Can you explain what problems you are having with the program in some more detail?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help

    I am a beginner my code does not work and then I do not know how to do. Can you help me to make the game and explain me the code please.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    my code does not work
    Please explain what "does not work" means.
    Are there compiler errors? Please copy the full text of the messages and paste them here.
    Is the program's output wrong? Please copy the contents of the command prompt window and paste it here.
    Add some comments describing what is wrong with the output and show what the output should be.

    Where is the Program class defined? I do not recognize it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help

    int rightprice (int Price, int RightPrice){
     
            while ((Price<RightPrice)||(Price>RightPrice)){
     
                if (Price<RightPrice){
    			    println("it's +");
    		    }
                else {
    			    println("it's -");
    		    }
            } return
        }

    I think it's not correct

    it's not int ... (int ..., int ...) what it is

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    I think it's not correct
    Please explain why you say that.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2021
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help

    because it returns Price + RightPrice

    and i don't want to do int + int

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    i don't want to do int + int
    What do you want instead?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2021
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help

    void rightprice (int Price, int RightPrice){
     
            while ((Price<RightPrice)||(Price>RightPrice)){
     
                if (Price<RightPrice){
    			    println("it's +");
    		    }
                else {
    			    println("it's -");
    		    }
            } 
    		println("You won in " + Counter + " times !");
        }


    --- Update ---

    class game extends Program{
     
        String name;
    	int Counter = 0;
     
        void intro(){
    	println("$$$$$$$$$$$$$$$$$$");
    	println("The price is right");
    	println("$$$$$$$$$$$$$$$$$$");
    	println();
    	print("Press enter to play");
    	readString();
    	clearScreen();
    	print("Enter your name : ");
    	name = readString();
    	clearScreen();
        }
     
     
        void rightprice (int Price, int RightPrice){
     
            while ((Price<RightPrice)||(Price>RightPrice)){
     
                if (Price<RightPrice){
    			    println("it's +");
    		    }
                else {
    			    println("it's -");
    		    }
            } 
    		println("You won in " + Counter + " times !");
        }
     
     
        int randNum(){
    	return (int) (0+random()*((200-0)+1));
        }
     
     
        /*int lireEntierSecurise(){
    	print(name + " : ");
    	String reponse = readString();
    	if (estNombreEntier(reponse)){
    	    return stringToInt(reponse);
    	}else{
    	    println("It's not a number "+ name +", try again");
    	    return lireEntierSecurise();
    	}
        }
     
        boolean estNombreEntier(String chaine){
    	boolean valide = true;*/
     
     
     
        void algorithm(){
            clearScreen();
            intro();
            println("You have to guess the price");
            //println(randNum());
            readInt();
        }
     
    }

    it says "You have to guess the price" and when you write a number it stops

    how can I improve my code
    Last edited by Alex123456; December 31st, 2021 at 01:00 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    How can I compile and execute the code for testing to see what happens?
    I do not recognize the Program class. I would need it to compile and test the code.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2021
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help

    I don't use the same java. Can you write your email then edit to send you the ap.jar?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    One big problem with the code is the infinite looping possible in the while loop:
    while ((Price<RightPrice)||(Price>RightPrice)){
    If the value(s) of either of those variables are not changed, then the loop will execute forever. The while loop needs to be large enough to include code that reads in a new value or changes the values somehow to allow the loop to exit.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Dec 2021
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help

    Can you write some code that I see

    Also how can I send you the ap.jar?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    Sorry, I do not write code for students to copy.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Dec 2021
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help

    I am trying to understand I am a beginner

    The program says "You have to guess the price" and when you write a number it stops

    readInt() when the user enters an integer, but what is it for "void rightprice (int Price, int RightPrice)" to read void?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    what is it for "void rightprice (int Price, int RightPrice)" to read void?
    Who wrote that code? Can you ask them what the code is for?

    Normally method names are verbs that say what action the method is going to take. I do not know what action rightprice is supposed to take.
    For example: readInt says it is going to read an int value, probably from a user. But what is done with the value that is read?

    A return type of void says the method is not going to return a value.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Dec 2021
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help

    void rightprice says if it's + or -

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    Why does rightprice have a loop? The name should be a verb, that describes what it does.
    For example: checkIfRightPrice
    Perhaps it should return true or false to tell its caller if the price was right.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: java help

    Here's a solution for your Java game:

    ```java
    import java.util.Scanner;

    class Game extends Program {

    String name;

    void intro(){
    println("$$$$$$$$$$$$$$$$$$");
    println("The price is right");
    println("$$$$$$$$$$$$$$$$$$");
    println();
    print("Press enter to play");
    readString();
    clearScreen();
    print("Enter your name : ");
    name = readString();
    clearScreen();
    }

    int rightPrice(int guess, int price){
    if (guess < price) {
    println("it's higher");
    } else if (guess > price) {
    println("it's lower");
    } else {
    println("Congratulations, " + name + "! You guessed the right price.");
    return 1; // Return 1 to indicate correct guess
    }
    return 0; // Return 0 for incorrect guess
    }

    void algorithm(){
    intro();
    println("You have to guess the price between 0 and 200");
    int price = randNum();
    int guess;
    int attempts = 0;
    while (true) {
    print(name + ", guess the price: ");
    guess = readInt();
    attempts++;
    if (rightPrice(guess, price) == 1) {
    println("It took you " + attempts + " attempts.");
    break;
    }
    }
    }

    int randNum(){
    return (int) (Math.random() * 201); // Generates a random number between 0 and 200
    }

    public static void main(String[] args) {
    Game game = new Game();
    game.algorithm();
    }
    }
    ```

    This solution defines a Java class `Game` with methods for the game logic. It provides an `intro()` method for the game introduction, a `rightPrice()` method to check the guessed price against the actual price, and an `algorithm()` method to run the game. The `randNum()` method generates a random number between 0 and 200. The game runs in a loop until the correct price is guessed, and it keeps track of the number of attempts.

    And remember, if you ever find yourself in need of extra guidance with your Java assignments or any programming challenges, there are numerous resources out there to lend a hand. Whether it's seeking assistance from online communities, exploring tutorial websites, or consulting with professional tutors, help is readily available. You might find useful support through platforms like programminghomeworkhelp.com. Keep learning and exploring the vast world of programming!

  20. #20
    Junior Member
    Join Date
    Nov 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help

    Quote Originally Posted by Alex123456 View Post
    Hello, I am a beginner and I need help with a game where you have to find the right price (the price is a random number).


    class game extends Program{
     
        String name;
     
        void intro(){
    	println("$$$$$$$$$$$$$$$$$$");
    	println("The price is right");
    	println("$$$$$$$$$$$$$$$$$$");
    	println();
    	print("Press enter to play");
    	readString();
    	clearScreen();
    	print("Enter your name : ");
    	name = readString();
    	clearScreen();
        }
     
        void testRightPrice(){
            int price = rightprice();
            assertTrue(0 <= price && price <= 200);
        }
     
        int rightprice (int Price, int RightPrice){
     
            while ((Price<RightPrice)||(Price>RightPrice)){
     
                if (Price<RightPrice){
    			    println("it's +");
    		    }
                else {
    			    println("it's -");
    		    }
            } return
        }
     
     
        int randNum(){
    	return (int) (0+random()*((200-0)+1));
        }
     
     
        /*int lireEntierSecurise(){
    	print(name + " : ");
    	String reponse = readString();
    	if (estNombreEntier(reponse)){
    	    return stringToInt(reponse);
    	}else{
    	    println("It's not a number "+ name +", try again");
    	    return lireEntierSecurise();
    	}
        }
     
        boolean estNombreEntier(String chaine){
    	boolean valide = true;*/
     
     
     
        void algorithm(){
            intro();
            println("You have to guess the price");
            println(rightprice());
        }
     
    }
    Good afternoon, everyone. I will be glad to recommend you this gaming site, because with this site you can easily and legally play all the new and interesting slot machines online - https://slotscity.ua/ru/game-hall . I liked to play on this site because it has the best gameplay and a lot of quality slot machines!
    Your game is almost there! I fixed a few things to make it run smoothly. Now, the game will prompt you for guesses and give feedback if you're too high or too low until you guess the right price. It’ll keep going until you get it right. Let me know if you need more help!

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java help

    I fixed a few things
    Can you point out what you fixed and why you fixed it? Sometimes explanations are useful. Just changing the code might not be enough.

    NB The original post is almost 3 years old. The OP will probably never come back to see what has been posted.
    If you don't understand my answer, don't ignore it, ask a question.