Java学习笔记2- If条件循环使用

来源:互联网 发布:有没有招淘宝客服的 编辑:程序博客网 时间:2024/06/07 16:05

/** 猜数字游戏

 * 1)在0-100内规定一个上限

 * 2)在规定的上限内随机产生一个数字

 *3)输入一个数字和随机数比较,如果比随机数大,显示Big;如果比随机数小,显示Small,如果和随机数相同,显示Bingo

 *4)   每输入一次统计次数!

 */



import java.util.Scanner;

import java.util.Random;


public class Ex2_0309 {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Please input the number: 0 -100");

int num =sc.nextInt();

Random random = new Random();

int number = random.nextInt(num);

System.out.println(number);

compare(number);


}

public static void compare(int number){

int times=0;

while(true){

Scanner sc =new Scanner(System.in);

System.out.print("Please input the number:");

int result = sc.nextInt();

times+=1;

if (result > number) {

System.out.println("You tried "+times+" times! "+"It is Big!\n");

continue;

}

if(result<number){

System.out.println("You tried "+times+" times! "+"It is Small!\n");

continue;

}

if(result==number) {

System.out.print("Bingo!");

System.out.println("You tried "+times+" times!");

break;

}

}

}


}

0 0
原创粉丝点击