#猜数游戏
package cn;
import java.util.Random;
import java.util.Scanner;
public class CS {
public static void main(String[] args) {
Random rand = new Random();
Scanner sc = new Scanner(System.in);
int[] lev = {100};
int max = lev[rand.nextInt(lev.length)];
int num = rand.nextInt(max) + 1;
String msg = "";
int index = 0;
while (true) {
++index;
System.out.println(" 请输入数字(1-" + max + " ");
int t = sc.nextInt();
if (t <= 0) {
msg = String.format("放弃游戏,程序退出。");
break;
}
if (t > num) {
msg = String.format("%d,太大了.", index);
} else if (t < num) {
msg = String.format("%d,太小了.", index);
} else if (t==num){
// msg = String.format("%d恭喜猜对了,你的游戏得分(%d分)", index, 110 - index * 10);
System.out.printf("%d,恭喜猜对了,你的游戏得分(%d分)", index, 110 - index * 10);
break;
}
System.out.println(msg);
}
// System.out.println("msg");
}
}
输出结果:
请输入数字(1-100
50
1,太大了.
请输入数字(1-100
40
2,恭喜猜对了,你的游戏得分(90分)
进程已结束,退出代码0
猜数游戏.
最新推荐文章于 2024-09-16 13:39:40 发布