import java.util.Scanner; public class demo1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub
一:选择定义该项目中所需要的变量:
Scanner input = new Scanner(System.in); String name; String dianname = null; int choose;//选择 int myfen;//本人得分 String kai;//开始 int dianfen;//电脑得分 int my;//本人 int dian;//电脑 int count = 0;//对战次数 int ying = 0;//本人赢得次数 int shu = 0;//本人输的次数 二:选择定义初始变量: System.out.println("---------欢迎进入游戏世界------------"); System.out.println("***************************"); System.out.println("\t\t**猜拳,开始**"); System.out.println("\t\t*****************"); boolean con = false;
三:选择本人要选择的对抗角色和输出的内容: do { con = false; System.out.println("出拳规则:1:剪刀 2:石头3:布"); System.out.println("请选择对方角色(1:刘备 2:孙权 3:曹操)"); choose = input.nextInt(); System.out.println("请输入姓名"); name = input.next(); switch (choose) { case 1: dianname = "刘备"; System.out.println(name + "VS" + dianname+"对战"); break; case 2: dianname = "孙权"; System.out.println(name + "VS" + dianname+"对战"); break; case 3: dianname = "曹操"; System.out.println(name + "VS" + dianname+"对战"); break; default: System.out.println("输入错误,请从新输入"); con = true; } } while (con); System.out.println("要开始吗(y/n)"); kai = input.next(); while (kai.equals("y")) { do{ con =false;
四:本人所输出的内容与人机所输出的内容的对抗与之比较: System.out.println("请出拳(1.拳头 2.剪刀 3.布)"); my = input.nextInt(); switch (my) { case 1: System.out.println(name + "出拳:石头"); break; case 2: System.out.println(name+"出拳:剪刀"); break; case 3: System.out.println(name+"出拳:布"); break; default: System.out.println("输入错误,请从新输入"); con = true; } }while(con); dian = (int) (Math.random() * 3 + 1); switch (dian) { case 1: System.out.println(dianname+" 出拳:石头 "); break; case 2: System.out.println(dianname+" 出拳:剪刀"); break; case 3: System.out.println(dianname+" 出拳:布"); break; }
六:不同的情况所显示的不同结果 if (my == 1 && dian == 1 || my == 2 && dian == 2 || my == 3 && dian == 3) { System.out.println("和局,真衰,嘿嘿,等着瞧吧"); } else if (my == 1 && dian == 2 || my == 2 && dian == 3 || my == 3 && dian == 1) { System.out.println("恭喜你赢了"); ying++; } else { System.out.println("你输了,真笨"); shu++; } count++; System.out.println("要继续吗(y/n)"); kai = input.next(); } System.out.println(name + "VS" + dianname); System.out.println("对战次数" + count); System.out.println("姓名/t得分"); System.out.println(name+"\t"+ying); System.out.println(dianname+"\t"+shu); if (ying > shu) { System.out.println("赢了"); } else if (ying < shu) { System.out.println("输了"); } else { System.out.println("和局"); } } }
注意:
1>要清楚该项目的循环内容,并使用正确的循环方法;
2>不同的情况所对的不同的结果;