Scanner input=new Scanner(System.in); String compername="刘备"; String compername1="孙权"; String compername2="曹操"; String name; int xuanze; public void top(){ System.out.println("***********欢迎进入游戏世界************"); System.out.println(" ***********"); System.out.println(" ***猜拳,开始***"); System.out.println(" ***********"); System.out.println("出拳规则:1.剪刀 2.石头 3.布"); System.out.println("请选择对方角色:(1.刘备 2.孙权 3.曹操):"); xuanze=input.nextInt(); System.out.println("请输入你的名字:"); name =input.next(); switch (xuanze){ case 1: System.out.println(name+"VS"+compername); break; case 2: System.out.println(name+"VS"+compername1); break; case 3: System.out.println(name+"VS"+compername2); break; } } public void zhong(){ int ren; int diannao; boolean y; String con ; int count = 0; int count1=0; int count2=0; System.out.println("要开始吗?(y/n)"); con=input.next(); while("y".equals(con)){ System.out.println("请出拳:1.剪刀 2.石头 3.布"); ren=input.nextInt(); if(ren==1){ System.out.println("你出拳 剪刀"); }else if(ren==2){ System.out.println("你出拳 石头"); }else { System.out.println("你出拳 布"); } diannao=(int)((Math.random())*3); switch(diannao){ case 0: System.out.println("电脑出拳 剪刀"); break; case 1: System.out.println("电脑出拳 石头"); break; case 2: System.out.println("电脑出拳 布"); break; } if((ren==1&&diannao==0)||(ren==2&&diannao==1)||(ren==3&&diannao==2)){ System.out.println("结果:和局,真衰!"); }else if((ren==1&&diannao==2)||(ren==2&&diannao==0)||(ren==3&&diannao==1)){ System.out.println("结果:恭喜,你赢了!"); count1++; }else { System.out.println("结果:你输了,真笨!!"); count2++; } System.out.println("要开始吗?(y/n)"); con=input.next(); count++; } System.out.println("**********************************"); if(xuanze==1){ System.out.println(compername+"VS"+name); }else if(xuanze==2){ System.out.println(compername1+"VS"+name); }else{ System.out.println(compername2+"VS"+name); } System.out.println("对战次数:"+count); if(count1>count2){ System.out.println("真厉害,奖励"); }else{ System.out.println("呵呵,真笨"); } System.out.println("**********************************"); }
调用代码
public static void main(String[] args) { Renji renji=new Renji(); renji.top(); renji.zhong(); }