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 9312309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:35:53+00:00 2026-06-19T01:35:53+00:00

My If statement aren’t giving me the write answer, for example if I input

  • 0

My “If” statement aren’t giving me the write answer, for example if I input “5” it prints too high which mean the random number is lower but if i input a lower number like “4” it prints too low which would mean the random number is higher. Its pretty confusing, Am just wonder if my logic in the code is correct??

import java.util.Scanner;
import java.lang.Math;

public class GuessTest
{

public static void main(String[] args) 
{
   System.out.println("Welcome to the gussing game, Try to guess the number am thinking of to win!");
   System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
   System.out.println();

    System.out.println("Am thinking of a number between 0 and 10. Try to guess it.");
    System.out.println();

   Scanner sc = new Scanner(System.in);
   String choice =  "y";

   while (!choice.equalsIgnoreCase("n"))
   {

     System.out.println("Enter the Number:");
       int guess = sc.nextInt();
       double rightNum = Math.random() *10;
       int randomNum = (int) rightNum;


       if (guess == randomNum)
       {
           System.out.println("Correct you've got it! The Number Was " + randomNum );
           System.out.println();
           System.out.println("Would you like to play again (y/n):");
           choice = sc.next();
       }
     else  if (guess < randomNum)
       {
           System.out.println("your too low!");
       }
     else if (guess > randomNum)
       {
            System.out.println("Your too high!");
       }   

}

}
}   
  • 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-19T01:35:55+00:00Added an answer on June 19, 2026 at 1:35 am

    You’re calculating the random number on each while-loop iteration. Just calculate it once by moving the calculation code out of the loop:

    double rightNum = Math.random() *10;
    int randomNum = (int) rightNum;
    while (!choice.equalsIgnoreCase("n")) {
        //your logic...
    }
    

    Since you want to have several game rounds, you can recalculate the random number when accepting a new game:

    double rightNum = Math.random() *10;
    int randomNum = (int) rightNum;
    while (!choice.equalsIgnoreCase("n")) {
        //your logic...
        if (guess == randomNum) {
            System.out.println("Correct you've got it! The Number Was " + randomNum );
            System.out.println();
            System.out.println("Would you like to play again (y/n):");
            choice = sc.nextLine();
            if (choice.equalsIgnoreCase("y")) {
                rightNum = Math.random() *10;
                randomNum = (int) rightNum;
            }
        }
        //your logic...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need write an update statement that used multiple tables to determine which rows
Problem Statement is : Given 2 Dimensional array, print output for example If 4
a) A SQL statement is a single SQL command (for example, SELECT * FROM
I have a SQLite database, which needs to be updated. Using the update statement
I have the following statement. The results come back but the records aren't sorted.
I'm using a php if statement to display a graphic when users aren't logged
If you've got a very complicated SELECT statement and some records aren't included because
I have a number of classes which have private member variables that implement IDisposable
Statement st=conn.createStatement(); String sql=SELECT a.ID,a.A_TEXT,a.B_TEXT,a.C,a.D_TO_E,a.F_TEXT,a.G,a.INTIME,b.EXIT_TIME FROM tm_A a LEFT JOIN tm_B b ON a.ID=b.ID
Statement: Static member variables can be ONLY changed by static methods. Is this statement

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.