栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java猜数字小游戏

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java猜数字小游戏

package pkg0;

import java.util.Scanner;//定义了这个用户才能输入

public class GuessNumber {
    static Scanner sc;
    static int targetNumber, userNumber;
    static int count;
    static int score;
    static String command;


    static void Initial() {
        count = 0;
        score = 0;
        targetNumber = (int)(Math.random()*100);
        userNumber   = 0;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Initial();

        System.out.println("1~100猜猜我想的是几");

        sc = new Scanner(System.in);//这个就是输入

        game:	do {
            Initial();
            while(true) {
                command = sc.nextLine();
                if (command.equals("cheat")) {
                    System.out.println(targetNumber);
                    continue;
                }
                else if (command.equals("stop"))
                    break game;
                userNumber = Integer.parseInt(command);//将String字符类型数据转换为Integer整型数据
                count ++;

                if (userNumber == targetNumber) {
                    //System.out.println("success");
                    score = 100 - (count - 1) * 5;
                    if (score >= 80)
                        System.out.println("你的分数是"+score+"  你这运气是天生的吗");
                    else if (score >= 40)
                        System.out.println("你的分数是"+score+"  多加练习啊");
                    else if (score >= 0)
                        System.out.println("你的分数是"+score+"  几天不见这么捞了");
                    else
                        System.out.println("你的分数是"+score+"  抓紧回家种地吧");
                    break;
                }
                else if (userNumber > targetNumber)
                    System.out.println("Your Number is bigger than i want.");
                else
                    System.out.println("Your Number is less than i want.");
            }
        }while (true);
        System.out.println("游戏结束");
        sc.close();
    }


}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号