Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8534593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:13:52+00:00 2026-06-11T10:13:52+00:00

Im brand new to java, and I’m writing this short program where you guess

  • 0

Im brand new to java, and I’m writing this short program where you guess a number between 1 and 10. The correct number is stored as an integer. If you guess a lower number, its supposed to say “The correct number is higher”, and if you guess a higher it should say “The correct number is lower”. Here’s what I have:

    import javax.swing.*;

public class Oppg3 {
    public static void main(String[] args) {
        int number = 7;
        int guessed = Integer.parseInt(JOptionPane.showInputDialog("Guess a number between 1 and 10"));
        while(guessed>number) guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a lower number."));
        while(guessed<number) guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a higher number."));
        JOptionPane.showMessageDialog(null, guessed + " is correct!");

    }

}

So obviously that wont work, cause if you enter a lower number it’ll jump to the next one, and that will come up as correct even if its higher. So how do I fix this so that it’ll check for both statements? Sorry for bad explaining. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:13:53+00:00Added an answer on June 11, 2026 at 10:13 am

    The simplest solution is to replace your current whiles with ifs, and contain them both in a

    while(guessed!=number)
    {
        if(guessed>number) //stuff
        if(guessd<number) //otherstuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm brand-spanking new to Java and I made this little translator for PigLatin. package
I am brand new to both java and to maven, so this is likely
I am brand new to Java and was given this piece of code, no
I'm brand new to java, coming from a ruby world. One thing I love
Brand new to Python (and programming in general), if this is simple and/or answered
I am a brand new Java developer (I have been working in asp.net) and
new to java and brand new to the site. I have a JLabel added
I am brand new to the Java EE world. As an exercise to try
Brand new to android and facing a weird problem.Check out the code below FirstActivity.java:
I'm brand new to Java, programming, and StackOverflow. I need to use a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.