练手小程序——Java猜拳游戏

版权声明:以上所有博客均为冷漠的小猿所有,引用请注明出处 https://blog.csdn.net/lyf1997115/article/details/81569612

猜拳游戏

  • 和电脑猜拳,一局定输赢(没事写着玩( • ̀ω•́ )✧)
import java.util.Random;
import java.util.Scanner;

public class Guess {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        guessGame();
    }
    public static void guessGame() {
        Scanner input = new Scanner(System.in) ;
        System.out.println("1.剪刀 2.石头 3.布");
        int user = input.nextInt() ;
        System.out.println("你" + print(user));
        Random random = new Random() ;
        int com = random.nextInt(3) + 1 ;
        System.out.println("电脑 " + print(com));
        int ch = user - com ;
        if (ch == 1 || ch == -2) {
            System.out.println("你赢了");
        }else if (ch == 0) {
            System.out.println("平局");
        }else {
            System.out.println("你输了");
        }
    }
    /**
     * 根据传入的选择,返回相应字符串
     * @param i
     * @return
     */
    public static String print(int i) {
        String str = "选择的是" ;
        if (i == 1) {
            str += "【剪刀】" ;
        }else if (i == 2) {
            str += "【石头】" ;
        }else{
            str += "【布】" ;
        }
        return str ;
    }
}

猜你喜欢

转载自blog.csdn.net/lyf1997115/article/details/81569612