What's new

Mga lods java help code

Alieson09

Forum Expert
Joined
Aug 30, 2021
Posts
2,246
Solutions
27
Reaction
2,961
Points
2,085
import java.util.Random;
import java.util.*;

public class PartThree
{

public static void main(String args[]){


int wrong = 0; //number of times guessed wrong
int attempts = 0; //total attempts
int keepGoing = 1;

System.out.println("Guess a number between 1 to 50!");
wrong++;
while (keepGoing == 1){
int x = 1;
Random rand = new Random();
int rnd = rand.nextInt(50); //computer's random number
do{
try{
Scanner scan = new Scanner(System.in);
int guessInt = scan.nextInt();

if (guessInt == rnd){
System.out.println("You got in " + wrong + " attempts");
wrong = 0;
}
else if (guessInt < rnd){
System.out.println("Too low. Try again " );
} else if (guessInt > rnd) {
System.out.println("Too high. Try again ");
}

}//try
catch(Exception e){//error handling
System.out.println("Follow the directions!");
}
}while(x == 1);//do (keep looping until directions are followed and x = 2)
} //while
} //method
} //class
1633838046984.png
bat ganyan lods hindi mabasa ang attempts kung mabasa man 1 nakalagay
 

Attachments

import java.util.Random;
import java.util.*;

public class PartThree
{

public static void main(String args[]){



int attempts = 0; //total attempts
int keepGoing = 1;

System.out.println("Guess a number between 1 to 50!");

while (keepGoing == 1){
int x = 1;
Random rand = new Random();
int rnd = rand.nextInt(50); //computer's random number
do{
try{
Scanner scan = new Scanner(System.in);
int guessInt = scan.nextInt();
attempts++;
if (guessInt == rnd){
System.out.println("You got in " + attempts + " attempts");
}
else if (guessInt < rnd){
System.out.println("Too low. Try again " );
} else if (guessInt > rnd) {
System.out.println("Too high. Try again ");
}

}//try
catch(Exception e){//error handling
System.out.println("Follow the directions!");
}
}while(x == 1);//do (keep looping until directions are followed and x = 1)
} //while
} //method
} //class
1633838573181.png

oks na mga lods haahhaa nakuha na
 

Attachments

import java.util.Random;
import java.util.*;

public class PartThree
{

public static void main(String args[]){



int attempts = 0; //total attempts
int keepGoing = 1;

System.out.println("Guess a number between 1 to 50!");

while (keepGoing == 1){
int x = 1;
Random rand = new Random();
int rnd = rand.nextInt(50); //computer's random number
do{
try{
Scanner scan = new Scanner(System.in);
int guessInt = scan.nextInt();
attempts++;
if (guessInt == rnd){
System.out.println("You got in " + attempts + " attempts");
}
else if (guessInt < rnd){
System.out.println("Too low. Try again " );
} else if (guessInt > rnd) {
System.out.println("Too high. Try again ");
}

}//try
catch(Exception e){//error handling
System.out.println("Follow the directions!");
}
}while(x == 1);//do (keep looping until directions are followed and x = 1)
} //while
} //method
} //class
View attachment 1659880
oks na mga lods haahhaa nakuha na
yung keepgoing variable hanging,
walang termination ang loop, only inputmismatchexception lang yata nakakapagterminate
 

Similar threads

Back
Top