0

My Code:

import java.util.*;

public class test
{

    public static void main(String[] args)
    {
        Scanner Input = new Scanner(System.in);

        System.out.println("Randomly put a ball to cup");
        int cupnumber = (int) ((Math.random()*6)+1);

        System.out.println("Guess where is it");
        int guess;
        guess = Input.nextInt();

        **while(cupnumber!=guess)
        {
            System.out.println("Guess a number");
                guess = Input.nextInt();
                guess(cupnumber,guess);
        }**
    }

    public static void guess(int cupnumber, int guess)
    {
            if(cupnumber == guess)
                System.out.print("Guess it correctly");
            else
                System.out.println("Try again");

    }

}

I am new to java programming. In the above code, without these bracket {} under while loop part, i cannot re-input a number if cupnumber doesn't not equal to guess. Yet, with these {} bracket under while loop, i can re-input a number if cupnumber doesn't not equal to guess.

Why does{} bracket make such a difference?

Can anyone help me? Thank you

Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
Ricky
  • 15
  • 1
  • 6
  • Brackets determine what's inside the loop - the statements that are repeated each time around. Without the brackets, only the first statement (the `System.out.println`) is. That is, `while (condition) something; somethingElse;` is equivalent to `while (condition) { something; } somethingElse;`. – Ry- Jul 23 '16 at 08:12

5 Answers5

1

It is a rather simple explanation. Without brackets, it will repeat the first line:

while(cupnumber!=guess)

    System.out.println("Guess a number");//Repeats this over and over
    guess = Input.nextInt();//These two are called outside the loop
    guess(cupnumber,guess);

But like this:

while(cupnumber!=guess)
{
    //Now all three lines are a part of the statement
    System.out.println("Guess a number");
    guess = Input.nextInt();
    guess(cupnumber,guess);
}

It says that do everything inside the brackets. Without brackets, only one line will be done action with, but with brackets everything inside the loop will be done.

This:

while(cupnumber!=guess)
    Single statement

Is a simple way to handle one-line if-statements(or while). However, brackets are required to make Java 'understand' that the if-statement or while-statement does several lines upon a true statement(if cupnumber != guess then the statement is true)

while (condition) {
     Several statements
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
0

Without the brackets, the next line of while statement is taken as while loop and other statements following are not part of loop. The next statement here means the statement which has the semicolon.

The reason for why you are not able to input another number when cupnumber is not equal input number, is you are running into an infinite loop, since while loop will be keep on running until the condition provided is true.

Without Brackets this is your while loop

while(cupnumber!=guess)
System.out.println("Guess a number");
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
0

The while statement continually executes a block of statements while a particular condition is true.

Its syntax can be expressed as:

while (condition) {
     statement(s)
}

In your code, as long as the cupnumber IS NOT EQUAL TO guess (condition becomes true), while loop goes inside the brackets.

Please find the below Oracle link for more understanding: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

Vasu
  • 21,832
  • 11
  • 51
  • 67
0
guess = Input.nextInt(); will not be called if it's not inside the bracket.

When you use curly brace, what's inside it is a block which will be treated as a single statement. So the below statements will be run one by one.

System.out.println("Guess a number");
guess = Input.nextInt();
guess(cupnumber,guess);

If there is no curly brace, only the first statement can be run right after the while condition definition.

Check Is there a difference in removing the curly braces from If statements in java for more details. Thx. Hope it will help.

Community
  • 1
  • 1
Eugene
  • 10,627
  • 5
  • 49
  • 67
0

this is happening because of the scope you defined with the {}

if you do this:

 while(cupnumber!=guess)
     System.out.println("Guess a number");
     guess = Input.nextInt();
     guess(cupnumber,guess);

the java is executing ONLY this statement System.out.println("Guess a number"); if the condition is met...

so you need to DEFINE A SCOPE of the code that is going to be repeated....

the same applies for if-else, for loops etc...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97