1

我知道他们有很多不同的方法来做到这一点,但我越来越擅长使用终端,并希望继续创建用户界面和桌面应用程序!我写了一个非常俗气和凌乱的“数字游戏”,并希望让它在一个带有按钮和所有爵士乐的小程序中运行!

public static void gameStart()  {
    System.out.println("Welcome to the Game of Awesome V1.2");
    System.out.println("-------------------------------------");
    spin();
}

public static void spin()   {

    int spinNum = (int)(10*Math.random());
    Scanner input = new Scanner(System.in);
    Scanner input2 = new Scanner(System.in);
    System.out.println("type a number between 1 and 10:");
    int guessNum = input.nextInt();

    if(guessNum > 10)   {
        System.out.println(guessNum + " is greater than 10 you fool!");
                    spin();
    }else if(guessNum < 1)  {
        System.out.println(guessNum + " is less than 1 you fool!");
                    spin();
    }else   {

                System.out.println("\nSweet, looks like you chose [" + guessNum + "], good luck...");
                System.out.println("\ntype \"s\" to spin and \"quit\" to, quit...");
                String run = input2.nextLine();
            switch (run.toLowerCase()) {
                case "s":
                    System.out.println("\nyou spun [" + spinNum + "] and guessed [" + guessNum + "]!");
                    if(guessNum == spinNum) {
                        win();
                        askPlay();
                    }else   {
                            lose();
                            askPlay();
                    }
                    break;
                case "quit":
                    System.out.println("See ya later!");
                    System.out.println();
                    System.exit(0);
                    break;
                default:
                    System.out.println("shit, you broke it! Luckily, I can fix this.");
                    askPlay();
                    System.out.println();
                    break;
            }
            }
}

public static void askPlay()    {

        Scanner input = new Scanner(System.in);
        System.out.println("\nWanna play again? (Type \"yes\" or \"no\")");
        String decideSpin = input.nextLine();

    switch (decideSpin.toLowerCase()) {
        case "yes":
            spin();
            break;
        case "no":
            System.out.println("\nI know it's a crappy game, thanks for playing though!");
            System.out.println();
            System.exit(0);
            break;
        default:
            System.out.println("\nI'm sorry,\nI was made by a lazy "
            + "programmer and can only understand \"yes\" or \"no\"..");
            askPlay();
            break;
    }
}

public static void win()    {
    System.out.println("\nWINNER: you won this insanley stupid game, of awesome! Be proud, winner. (:");
}

public static void lose()   {
    System.out.println("\nLOOOOOSSSSSEEEERRRR: yep, you stand with the majority with this loss.");
}

我怎样才能做到这一点?

4

4 回答 4

2

我建议您先阅读Swing Trail

您需要了解的另一件事是 Swing 是事件驱动的,不像控制台程序,它倾向于按顺序运行

于 2012-09-17T00:49:11.250 回答
1

嗯,很简单:开始学习一些 Java 的 GUI,我会推荐以下两个之一:

于 2012-09-17T00:31:59.203 回答
1

一定要开始学习 Swing。这是标准的 Java GUI 框架。

于 2012-09-17T00:45:29.987 回答
0

考虑在处理(Java 库/Applet)中运行它。这可能是最快最简单的方法来启动和运行。如果您愿意,您甚至可以将其导出为应用程序。干杯!

于 2013-09-04T18:32:34.080 回答