Skip to main content

New to coding - please help

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
Sachin95
Offline
Joined: 2013-03-27

Hi guys I am fairly new to coding and am taking a computer science course. I am working on an assignment during my spare time and am not sure if what I'm doing is even correct but I'm just making a very BASIC code. The purpose is to guess a random number generated by the computer and each time you guess it tells you if you are above or below and how many tries you have left. I am using basic "while" statements and I'm not even sure if you can put a while statement inside another while statement.. I tried so please recommend me any videos and tutorials to get me started. Here is my code so any help to fix it would be appreciated!.
---------------------------------------------------------------------------
import java.util.Scanner;

public class Guess
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int estimate,count,randNum;
estimate = 0;
count = 5;
randNum = (int)(10*Math.random() +1);
while (count>0);{{{{
count = count-1;
System.out.print("Guess my random number (1-10):");
estimate = input.nextInt();
}while (estimate>randNum);
System.out.println("Too big!");
System.out.println("Number of tries left:" + count);
}while (estimate System.out.println("Too small!");
System.out.println("Number of tries left:" + count);
}while (estimate==randNum);
System.out.println("Correct!");
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
MackSix
Offline
Joined: 2013-03-27

You should use the code tags (button with "<>" on it) when you post code so it is readable.

1st, your code is broken and missing a closing ")".
2nd, you are using the wrong type as a condition in one of the while loops.

public class Guess {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int estimate, count, randNum;
        estimate = 0;
        count = 5;
        randNum = (int) (10 * Math.random() + 1);
        while (count > 0);
        {
            {
                {
                    {
                        count = count - 1;
                        System.out.print("Guess my random number (1-10):");
                        estimate = input.nextInt();
                    }
                    while (estimate > randNum);
                    System.out.println("Too big!");
                    System.out.println("Number of tries left:" + count);
                }
                while (estimate  {
                    System.out.println("Too small!");
                }
                System.out.println("Number of tries left:" + count);
            }
            while (estimate == randNum);
            System.out.println("Correct!");
        }
    }

}