Java猜字游戏,求大神源代码,最好有注释,谢谢
猜数字游戏,其游戏规则为:程序内自动生成一个 1 到 100 之间的数字作为猜测的结果,由用户猜测此数字。用户每猜测一次,由系统提示猜测结果:大了、小了或者猜对了。玩家最多猜5次,猜对结果,则提示You Win,没猜对则提示Game Over。用户可以提前退出游戏,即,游戏过程中,如果用户录入数字0,则游戏终止并提示放弃游戏。求大神源代码 import java.io.*;
import java.util.*;
class test
{
public static void main (String[] args) throws java.lang.Exception
{
int num,guess=-1;
Scanner sc = new Scanner(System.in);
int time=5;
Random r = new Random();
num = r.nextInt(99)+1;
System.out.print("请输入一个1~100之间的整数:");
guess=sc.nextInt();
while(--time>0&&guess!=0)
{
if (guess==num){
System.out.println("猜对了!");
break;
}
else if(guess<num){
System.out.println("小啦,小啦!");
}
else if(guess>num){
System.out.println("大啦,大啦!");
}
System.out.print("还有"+time+"次机会:");
guess=sc.nextInt();
}
if (guess==num){
System.out.println("You win!");
}
else {
System.out.println("Game Over!");
}
}
}
页:
[1]