Discuss / Java / 作业

作业

Topic source

Afu

#1 Created at ... [Delete] [Delete and Lock User]
System.out.println("石头剪刀布游戏,请输入你的选择:");
System.out.println("1.石头");
System.out.println("2.剪刀");
System.out.println("3.布");
int player = scanner.nextInt();
//电脑随机生成一个数
int computer = (int)(Math.random() * 3 + 1);
System.out.println("你的选择是:" + player + ",电脑的选择是:" + computer);
//判断输赢
switch (computer - player) {
    case 0:
        System.out.println("平局");
        break;
    case 1:
    case -2:
        System.out.println("你赢了");
        break;
    case -1:
    case 2:
        System.out.println("你输了");
        break;
}

这思路没有考虑到玩家输入1-3以外的数字


  • 1

Reply