該如何實現輸出的是:你出的是剪刀 ,電腦出的布、這樣的形式呢。我目前只能以數字 代替。
代碼如下:求大神指點、
import java.util.*;
public class A3_10{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
while(true){
System.out.println("=======猜拳游戲=====");
System.out.println("請出拳:(1.剪刀 2.石頭 3.布)");
int number=in.nextInt();
if(number>3||number<1){
System.out.println("你出拳作弊,請重新合法出拳");
}else{
int number1=(int)((Math.random()*3)+1);
if(number==number1){
System.out.println("你出的是"+number+"\t電腦出的是"+number1+"\t本局平局");
}else if(number<number1){
System.out.println("你出的是"+number+"\t電腦出的是"+number1+"\t本局你輸");
}else{
System.out.println("你出的是"+number+"\t電腦出的是"+number1+"\t本局你贏");
}
}
}
}
}
剛才復制沒完全...
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("=======猜拳游戲=====");
System.out.println("請出拳:(1.剪刀 2.石頭 3.布)");
int number = in.nextInt();
if (number > 3 || number < 1) {
System.out.println("你出拳作弊,請重新合法出拳");
} else {
int number1 = (int) ((Math.random() * 3) + 1);
if (number == number1) {
System.out.println("你出的是" + getName(number) + "\t電腦出的是" + getName(number1)
+ "\t本局平局");
} else if (number < number1 || number==3&&number1==1) {
System.out.println("你出的是" + getName(number) + "\t電腦出的是" + getName(number1)
+ "\t本局你輸");
} else {
System.out.println("你出的是" + getName(number) + "\t電腦出的是" + getName(number1)
+ "\t本局你贏");
}
}
}
}
public static String getName(int n){
String s = null;
switch(n){
case 1: s = "剪刀"; break;
case 2: s = "石頭"; break;
case 3: s = "布"; break;
}
return s;
}
}