java猜数字游戏

import java.util.Scanner;

//导入包

class GuessNum
{
public static void main(String[] args)
{
int num = (int)(Math.random()*100)+1;

//获取随机数
int times=0;
while(times<5)
{
times++;
System.out.println("剩余次数:"+(5-times));
System.out.println("请输入你猜测的数字");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
if (num>i)
{
System.out.println("你猜的"+i+"小了");

}
else if(num<i)
{
System.out.println("你猜的"+i+"大了");

}
else
System.out.println("恭喜你猜中了");

}
if(times==5)
System.out.println("你输了");
}
}

posted @ 2016-12-25 14:29  过不去的过去  阅读(281)  评论(0编辑  收藏  举报