Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class SuggestNumber{
- public static void main(String[] argv){
- Scanner sc = new Scanner(System.in);
- Random r = new Random();
- System.out.println("Game suggest number");
- int maxTry = 8;
- int count = 0;
- System.out.println("Please guess the number between 1 - 100");
- int num = r.nextInt(100);
- int minNum = 1;
- int maxNum = 100;
- String answer = "";
- String a = "123";
- while(true){
- //count = count + 1;
- count++;
- System.out.println("It is " + num + "? (> < =)");
- answer = sc.nextLine();
- if(answer.equals(">")){
- minNum = num;
- }else if(answer.equals("<")){
- maxNum = num;
- }else if(answer.equals("=")){
- System.out.println("Computer won in " + count + " attemps!");
- break;
- }else{
- System.out.println(count);
- }
- if(count >= maxTry){
- System.out.println("User won!");
- break;
- }
- num = minNum + r.nextInt(maxNum-minNum);
- }
- }
- }
Add Comment
Please, Sign In to add comment