Java 实现 四位猜数字 小程序

代码:
package caishuzi;

import java.util.Scanner;

public class demo {

public static void main(String[] args) {
	int random1=(int)(Math.random()*10000);
	System.out.println(random1);//
	Scanner scanner = new Scanner(System.in);
	int inpput;
	boolean gameover = false;	
	while (!gameover) {	
		System.out.print("請輸入四個數字");
		if(!scanner.hasNextInt()) {
			System.out.println("输入的不是数字");
			scanner.close();
			continue;
			
		}
	inpput = scanner.nextInt();
		if(inpput<1000 || inpput>10000) {
			System.out.println("輸入的不是四位数字");
			continue;//
		}
		if (random1 > inpput) {
			System.out.println("你输入的数字小了");
			} 
		else if (random1 < inpput) {
			System.out.println("你输入的数字大了");
		} else {
			System.out.println("输入正确");
			gameover = true;
		}
	}

	scanner.close();
}
	}

重点:
1.理解while循环
2.能正确认识并使用 break, continue,return ;
**break–退出while循环,但while循环后面还有其他语句的话,还是会执行

continue–退出当次while,会接着从while循环的开始部分[重新执行]下来,//可以用continue 实现猜四位数字游戏,循环进入

return–退出函数,直接返回主函数从新开始,while循环后面还有其他语句的话,直接跳过不会执行****

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值