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 2 of 2

Thread: Need HELP i don't know what's wrong with the code

  1. #1
    Junior Member
    Join Date
    Jun 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Need HELP i don't know what's wrong with the code

    import java.swing.*;

    public class GuessingGame {
    public static void main(String[] args) {
    int computerNumber = (int) (Math.random()*100 + 1);
    int userAnswer = 0;
    System.out.println("The correct guess would be " + computerNumber);
    int count = 1;

    while (userAnswer != computerNumber)
    {
    String response = JOptionPane.showInputDialog(null)
    "Enter a guess between 1 and 100", "Guessing Game", 3);
    userAnswer = Integer.parseInt(response);
    JoptionPane.showMessageDialog(null, ""+ determineGuess(userAnswer, computerNumber, count));
    count++;
    }
    }

    public static String determineGuess(int userAnswer, int computerNumber, int count){
    if (userAnswer <=0 || userAnswer >100) {
    return "Your guess is invalid";
    }
    else if (userAnswer == computerNumber ){
    return "Correct!\nTotal Guesses: " + count;
    }
    else if (userAnswer > computerNumber) {
    return "Your guess is too high, try again.\nTry Number: " + count;
    }
    else if (userAnswer < computerNumber) {
    return "Your guess is too low, try again.\nTry Number: " + count;
    }
    else {
    return "Your guess is incorrect\nTry Number: " + count;
    }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need HELP i don't know what's wrong with the code

    Please explain why you think something is wrong.
    Copy the full text of any error messages and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. what is wrong with my code?
    By matanbn in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 19th, 2019, 02:51 PM
  2. What's wrong with this code
    By easypzz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 13th, 2019, 07:51 AM
  3. What is wrong with my I/O code?
    By shimazaky in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 29th, 2013, 07:32 AM
  4. What is wrong with my code?
    By unleashed-my-freedom in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2012, 12:41 AM
  5. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM