題目
寫一個三人的剪刀石頭布遊戲(其中一個可以由自己輸入)
package ch5;
import java.io.*;
public class ch5_18
{
public static void main(String args[])throws IOException
{/*題目說可以自己出,全部電腦出來省時間
InputStreamReader in=new InputStreamReader(System.in);
BufferedReader buf=new BufferedReader(in);
*/
int a,b,c;
String sa="未知",sb="未知",sc="未知",res="無法判斷";
System.out.println("輸入0等於剪刀1等於石頭2等於布");
/*
String sa;
sa=buf.readLine();
a=Integer.parseInt(sa);
*/
a=(int)(Math.floor(Math.random()*3));
b=(int)(Math.floor(Math.random()*3));
c=(int)(Math.floor(Math.random()*3));
switch (a)
{
case 0:
sa="剪刀";
break;
case 1:
sa="石頭";
break;
case 2:
sa="布";
break;
}
switch (b)
{
case 0:
sb="剪刀";
break;
case 1:
sb="石頭";
break;
case 2:
sb="布";
break;
}
switch (c)
{
case 0:
sc="剪刀";
break;
case 1:
sc="石頭";
break;
case 2:
sc="布";
break;
}
/*以上拉出來判斷出的東西*/
switch (a)
{
case 0:
switch (b)
{
case 0:
switch (c)
{
case 0:
res = "平手在猜一次";
break;
case 1:
res = "C獲勝";
break;
case 2:
res = "A和B換勝";
break;
}
break;
case 1:
switch (c)
{
case 0:
res = "B獲勝";
break;
case 1:
res = "B和C獲勝";
break;
case 2:
res = "在猜一次";
break;
}
break;
case 2:
switch (c)
{
case 0:
res = "A和C獲勝";
break;
case 1:
res = "在猜一次";
break;
case 2:
res = "A獲勝";
break;
}
break;
}
break;
case 1:
switch (b)
{
case 0:
switch (c)
{
case 0:
res = "A獲勝";
break;
case 1:
res = "A和C獲勝";
break;
case 2:
res = "在猜一次";
break;
}
break;
case 1:
switch (c)
{
case 0:
res = "C獲勝";
break;
case 1:
res = "平手在猜一次";
break;
case 2:
res = "C獲勝";
break;
}
break;
case 2:
switch (c)
{
case 0:
res = "在猜一次";
break;
case 1:
res = "B獲勝";
break;
case 2:
res = "B和C獲勝";
break;
}
break;
}
break;
case 2:
switch (b)
{
case 0:
switch (c)
{
case 0:
res = "B和C獲勝";
break;
case 1:
res = "在猜一次";
break;
case 2:
res = "B獲勝";
break;
}
break;
case 1:
switch (c)
{
case 0:
res = "在猜一次";
break;
case 1:
res = "A獲勝";
break;
case 2:
res = "A和C獲勝";
break;
}
break;
case 2:
switch (c)
{
case 0:
res = "C獲勝";
break;
case 1:
res = "A和B獲勝";
break;
case 2:
res = "平手在猜一次";
break;
}
break;
}
break;
}
System.out.println("結果是"+res+",A和B和C分別出"+sa+","+sb+","+sc);
}
}
==============
輸入0等於剪刀1等於石頭2等於布
結果是在猜一次,A和B和C分別出布,石頭,剪刀
留言列表