猜数游戏.


#猜数游戏

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值