Discuss / Java / 打卡

打卡

Topic source
package com.testted;import java.util.Scanner;/** * switch实现石头/剪子/布并判断胜负 */public class Main {    public static void main(String[] args) {        System.out.println("请输入你的选择:");        System.out.println(" 1: 石头");        System.out.println(" 2: 剪刀");        System.out.println(" 3: 布");        String [] result = {"石头","剪刀","布"};        // 用户输入:        int choice =0 ;        while (choice!=1&&choice!=2&&choice!=3){            System.out.println("请输入:1,2,3中的其中一个数字");            Scanner a = new Scanner(System.in);            choice = a.nextInt();        }        System.out.println("您的选择为"+result[choice-1]);        // 计算机随机数 1, 2, 3:        int random = 1 + (int) (Math.random() * 3);        System.out.println("电脑的选择为"+result[random-1]);        int b = random - choice;        switch (b) {            // TODO:            case 2:                System.out.println("你输了!");                break;            case 1:                System.out.println("你赢了!");                break;            case 0:                System.out.println("平局");                break;            case -1:                System.out.println("你输了!");                break;            case -2:                System.out.println("你赢了!");                break;            default:                System.out.println("请输入:1,2,3 中的一个数字");        }    }}

package com.testted;

import java.util.Scanner;

/**

 * switch实现石头/剪子/布并判断胜负

 */

public class Main {

    public static void main(String[] args) {

        System.out.println("请输入你的选择:");

        System.out.println(" 1: 石头");

        System.out.println(" 2: 剪刀");

        System.out.println(" 3: 布");

        String [] result = {"石头","剪刀","布"};

        // 用户输入:

        int choice =0 ;

        while (choice!=1&&choice!=2&&choice!=3){

            System.out.println("请输入:1,2,3中的其中一个数字");

            Scanner a = new Scanner(System.in);

            choice = a.nextInt();

        }

        System.out.println("您的选择为"+result[choice-1]);

        // 计算机随机数 1, 2, 3:

        int random = 1 + (int) (Math.random() * 3);

        System.out.println("电脑的选择为"+result[random-1]);

        int b = random - choice;

        switch (b) {

            // TODO:

            case 2:

                System.out.println("你输了!");

                break;

            case 1:

                System.out.println("你赢了!");

                break;

            case 0:

                System.out.println("平局");

                break;

            case -1:

                System.out.println("你输了!");

                break;

            case -2:

                System.out.println("你赢了!");

                break;

            default:

                System.out.println("请输入:1,2,3 中的一个数字");

        }

    }

}

YANGZY1202

#3 Created at ... [Delete] [Delete and Lock User]

回复的时候选择回复框工具栏里的代码按钮“</>”,可以输出代码框,排版会更好看。


  • 1

Reply