class Myexception extends Exception {
public static void compute(int a) throws Myexception
{
if(!(a==1 || a==2 || a==3))
{
throw new Myexception();
}
}
}
/**
* @(#)GuessFist.java
*
* GuessFist application
*
* @author kuroro
* @version 1.00 2011/9/9
*/
import java.util.Scanner;
public class GuessFist {
public static void main(String[] args) {
int playernum1= 0;
System.out.println("欢迎来到猜拳游戏");
System.out.println("------------------------");
do
{ System.out.println("请选择一个角色进行游戏");
System.out.println("1.孙权");
System.out.println("2.刘备");
System.out.println("3.曹操");
System.out.println("请玩家输入所选角色对应的数字");
try{
Scanner enter = new Scanner(System.in);
playernum1 = enter.nextInt();
Myexception.compute(playernum1);
break;
}catch(Myexception ex){
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}catch(Exception ex){
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}
}while(true);
switch(playernum1)
{
case 1:
System.out.println("玩家选择的角色是孙权");break;
case 2:
System.out.println("玩家选择的角色是刘备");break;
case 3:
System.out.println("玩家选择的角色是曹操");break;
}
System.out.println("------------------------");
int computernum = (int)(Math.random()*3)+1;
switch(computernum)
{
case 1:
System.out.println("电脑选择的角色是孙权");break;
case 2:
System.out.println("电脑选择的角色是刘备");break;
case 3:
System.out.println("电脑选择的角色是曹操");break;
}
for(int i=0; i<=3;i++)
{
System.out.println("------------------------");
System.out.println("1.剪刀");
System.out.println("2.石头");
System.out.println("3.布");
int fist1 = 0;
do{
try{
System.out.println("请玩家出拳(输入对应的数字)");
fist1 = (new Scanner(System.in).nextInt());
Myexception.compute(fist1);
break;
}catch(Myexception ex){
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}catch(Exception ex){
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}
}while(true);
String playerfist = fist1==1?"剪刀":fist1==2?"石头":"布";
int fist2 = (int)(Math.random()*3)+1;
String computerfist = fist2==1?"剪刀":fist2==2?"石头":"布";
System.out.println("------------------------");
System.out.println("玩家出的是"+playerfist);
System.out.println("电脑出的是"+computerfist);
if((fist1==1&&fist2==3)||(fist1==2&&fist2==1)||(fist1==3&&fist2==2))
{
System.out.println("恭喜!本局玩家获胜");
}
else if((fist1==1&&fist2==1)||(fist1==2&&fist2==2)||(fist1==3&&fist2==3))
{
System.out.println("残念!本局双方握手言和");
}else
{
System.out.println("胜败乃兵家常事,大侠请重新来过!");
}
}
}
}
修改了部分代码可以显示玩家选得角色名
/**
* @(#)GuessFist.java
*
* GuessFist application
*
* @author kuroro
* @version 1.00 2011/9/9
*/
import java.util.Scanner;
public class GuessFist {
public static void main(String[] args) {
int playernum1= 0;
String playername="";
String computername="";
System.out.println("欢迎来到猜拳游戏");
System.out.println("------------------------");
do
{ System.out.println("请选择一个角色进行游戏");
System.out.println("1.孙权");
System.out.println("2.刘备");
System.out.println("3.曹操");
System.out.println("请玩家输入所选角色对应的数字");
try{
Scanner enter = new Scanner(System.in);
playernum1 = enter.nextInt();
Myexception.compute(playernum1);
break;
}catch(Myexception ex){
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}catch(Exception ex){
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}
}while(true);
switch(playernum1)
{
case 1:
playername= "孙权";
System.out.println("玩家选择的角色是孙权");break;
case 2:
playername= "刘备";
System.out.println("玩家选择的角色是刘备");break;
case 3:
playername= "曹操";
System.out.println("玩家选择的角色是曹操");break;
}
System.out.println("------------------------");
int computernum = (int)(Math.random()*3)+1;
switch(computernum)
{
case 1:
computername= "孙权";
System.out.println("电脑选择的角色是孙权");break;
case 2:
computername= "刘备";
System.out.println("电脑选择的角色是刘备");break;
case 3:
computername= "曹操";
System.out.println("电脑选择的角色是曹操");break;
}
for(int i=0; i<=3;i++)
{
System.out.println("------------------------");
System.out.println("1.剪刀");
System.out.println("2.石头");
System.out.println("3.布");
int fist1 = 0;
do{
try{
System.out.println("请"+playername+"出拳(输入对应的数字)");
fist1 = (new Scanner(System.in).nextInt());
Myexception.compute(fist1);
break;
}catch(Myexception ex){
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}catch(Exception ex){
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}
}while(true);
String playerfist = fist1==1?"剪刀":fist1==2?"石头":"布";
int fist2 = (int)(Math.random()*3)+1;
String computerfist = fist2==1?"剪刀":fist2==2?"石头":"布";
System.out.println("------------------------");
System.out.println(playername+"出的是"+playerfist);
System.out.println(computername+"出的是"+computerfist);
if((fist1==1&&fist2==3)||(fist1==2&&fist2==1)||(fist1==3&&fist2==2))
{
System.out.println("恭喜!本局玩家选的"+playername+"获胜");
}
else if((fist1==1&&fist2==1)||(fist1==2&&fist2==2)||(fist1==3&&fist2==3))
{
System.out.println("残念!"+playername+computername+"双方握手言和");
}else
{
System.out.println("玩家选择的"+playername+"输了T.T"+"胜败乃兵家常事,大侠请重新来过!");
}
}
}
}