Iteration Lesson Hacks
My attempt at the Iteration Lesson hw
import java.util.*;
import java.lang.Math;
public class GuesserGame {
private int secret;
private boolean game = false;
public void guess(){
secret = (int) (Math.random() * 500);
int num = 0;
int guesscount = 0;
while (!game){
System.out.println("Guess a number 1-500: ");
// Scanner for int
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
System.out.print("" + num + " - ");
// increment guesscount and check if guess is correct
guesscount++;
if (num == secret) {
System.out.println("Congratulations! You got it right in " + guesscount + " guesses !");
game = true;
}
else if (num > secret) {
System.out.println("Your guess is too high!");
}
else if (num < secret) {
System.out.println("Your guess is too low!");
}
}
}
public void guess(int num){
}
}
GuesserGame a = new GuesserGame();
a.guess();