Discuss / Java / 交作业

交作业

Topic source

Worm

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

package com.itranswarp.learnjava;

import java.util.Scanner;

/**

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

*/

public class Main {

public static void main(String[] args) {

System.out.println("please choice:");

System.out.println(" 1: Rock");

System.out.println(" 2: Scissors");

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

// 用户输入:

Scanner scanner = new Scanner(System.in);

int choice = scanner.nextInt();

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

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

int compu=random%3+1;

switch ((choice-compu)) {

// TODO:

case 0->System.out.println("equal");

case -1->System.out.println("win");

case -2->System.out.println("lose");

default ->System.out.println("error");

};

}

}

d

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

System.out.println("""
1. Rock
2. Scissors
3. Paper
""");
System.out.println("请输入的你的选择:\n");
// 设置输入
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
int random = 1+ (int)(Math.random()*3);
System.out.println("你的选择是"+GetGameChoice(choice));
System.out.println("电脑的选择是"+GetGameChoice(random));
if (GetGameChoice(choice) == "无效选择"){
System.out.println("退出游戏");
}else if (choice == random){
System.out.println("平局");
}else{
switch (choice){
case 1 : // 用户石头
if (random == 2){ // 电脑剪刀
System.out.println("你赢了");
}else {
System.out.println("你输了");
}
break;
case 2 : // 用户剪刀
if (random == 1){ //电脑选择布
System.out.println("你输了");
}else
System.out.println("你赢了");
case 3 : //用户布
if(random == 1){ // 电脑选择石头
System.out.println("你赢了");
}else
System.out.println("你输了");
}
}

}
public static String GetGameChoice(int choice){
switch (choice){
case 1 :
return "Rock";
case 2 :
return "Scissors";
case 3:
return "Paper";
default:
return "无效选择";
}
}


  • 1

Reply