What's new

Java code help po

Alieson09

Limited Access
Joined
Aug 30, 2021
Posts
775
Solutions
1
Reaction
1,430
Age
24
mga lods pano ilagay dyan ung try and catch nagawa kona code pero walang try and catch mga lods
badly need help

1633788960342.png
 

Attachments

Last edited:
san dyna lods after ng while sa baba??
Kung ipopost mo sana codes mas madali =D

after ng win = true;
lagay ka ng break para mag break yung loop

Saka yung mga message mo lagay mo sa every after the statement

like yung You win! dapat nandun yan sa

win = true;
you win!
break;
 

Alieson09

Limited Access
Kung ipopost mo sana codes mas madali =D

after ng win = true;
lagay ka ng break para mag break yung loop

Saka yung mga message mo lagay mo sa every after the statement

like yung You win! dapat nandun yan sa

win = true;
you win!
break;
1633792938753.png

1633792952907.png

lods pano hindi malabas yang you win pag invalid??
 

Attachments

Alieson09

Limited Access
only put break sa win = true
since you want to try again di mo need mag break dun sa try again

post mo yung codes mismo.

saka di mo need maglagay ng try and catch diyan.
May while statement ka naman. Pero kung need mo talaga. Make sure to put a break din after the exception message
need lods yan kasi nakalaagy sa instruction kung wala lang sana yan edi tapos na code ko hehehe
 

Alieson09

Limited Access
Pakipost yung codes wag screenshot.
Wag yung whole project ayang main codes lang
import java.util.Random;
import java.util.*;

public class GuessingGame {

public static void main(String[] args) {

Random rand = new Random();
int numberToGuess = rand.nextInt(50);
int numberOfTries = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;

while (win == false) {

System.out.println("Guess a number between 1 to 50: ");
guess = input.nextInt();
numberOfTries++;

if (guess == numberToGuess) {
win = true;
}
else if (guess < numberToGuess) {
System.out.println("Too low. Try again");
}
else if (guess > numberToGuess) {
System.out.println("Too high. Try again");
}
}
System.out.println("\t");
System.out.println("You win!");
System.out.println("The number was " + numberToGuess);
System.out.println("It took you " + numberOfTries + " tries ");
}
}
 
ayan lods
Java:
import java.util.Random;
import java.util.*;

public class GuessingGame {
    public static void main(String[] args) {

        Random rand = new Random();
        int numberToGuess = rand.nextInt(50);
        int limitOftries = 10;
        int numberOfTries = 0;
        Scanner input = new Scanner(System.in);
        int guess;
        boolean win = false;

        while (!win) {
            try {
                System.out.println("Guess a number between 1 to 50: ");
                guess = input.nextInt();
                numberOfTries++;
  
                if (guess == numberToGuess) {
                    System.out.println("You win!");
                    win = true;
                    break;
                } else if (guess < numberToGuess) {
                    System.out.println(numberToGuess + " is too low. Try again");
                } else if (guess > numberToGuess) {
                    System.out.println(numberToGuess + " is too high. Try again");
                }else if (numberOfTries == limitOftries){
                    System.out.println("It took you " + numberOfTries + " tries already. Exiting.");
                    break;
                }
            }catch(Exception e){
                break;
            }
        }
    }
}


Ikaw na bahala kung saan mo dapat ilagay yung number of retries message
pwde mo rin lagyan ng else kung gusto mo mag break agad.
for example may limit lang yung retries like 10
kapag failed parin, mabebreak dapat
 
Last edited:

Alieson09

Limited Access
Java:
import java.util.Random;
import java.util.*;

public class GuessingGame {
    public static void main(String[] args) {

        Random rand = new Random();
        int numberToGuess = rand.nextInt(50);
        int numberOfTries = 0;
        Scanner input = new Scanner(System.in);
        int guess;
        boolean win = false;

        while (!win) {
            try {
                System.out.println("Guess a number between 1 to 50: ");
                guess = input.nextInt();
                numberOfTries++;
 
                if (guess == numberToGuess) {
                    System.out.println("You win!");
                    win = true;
                    break;
                } else if (guess < numberToGuess) {
                    System.out.println("Too low. Try again");
                    System.out.println("The number was " + numberToGuess);
                } else if (guess > numberToGuess) {
                    System.out.println("Too high. Try again");
                    System.out.println("The number was " + numberToGuess);
                }
            
            }catch(Exception e){
                System.out.println("It took you " + numberOfTries + " tries ");
                break;
            }
        }
    }
}

View attachment 1659386


Ikaw na bahala kung saan mo dapat ilagay yung number of retries message
pwde mo rin lagyan ng else kung gusto mo mag break agad.
for example may limit lang yung retries like 10
kapag failed parin, mabebreak dapat
1633796774174.png

lods pano babalik sa guess a number?? pag ganyan??
 

Attachments

Online statistics

Members online
1,718
Guests online
1,688
Total visitors
3,406

Forum statistics

Threads
1,233,698
Posts
21,653,759
Members
1,616,417
Attachments
1,738,293
Profile posts
100,319
Top