标签:OLE case 数字 scanner int 定义 电脑 世界 dom
package java12; import java.util.Scanner; public class renjicaiquan { Scanner input = new Scanner(System.in); //定义玩家类的属性 String name; int score; String action; int num; //定义玩家类的猜拳方法 public void show() { System.out.println("\n请出拳:1.剪刀 2.石头 3.布"); boolean a = true; do { num = input.nextInt(); if (num == 1 || num == 2 || num == 3) { switch (num) { case 1:
public class renjicaiquan1 { //电脑类 String name; int score; String action; int sum; //定义电脑类猜拳方法 public void show() { //产生随机数 sum=(int)(Math.random()); switch(sum) { case 1: action="剪刀"; break; case 2: action="石头"; break; case 3: action="布"; break; } System.out.println(name+"出拳"+action); } }
import java.util.Scanner; public class renjicaiquan2 { //游戏类 Scanner input=new Scanner(System.in); //定义游戏类的属性 //创建玩家类的对象 renjicaiquan it=new renjicaiquan(); //创建电脑类的对象 renjicaiquan1 is=new renjicaiquan1(); int number; int witch=0; //定义游戏进程的方法 public void process() { System.out.println("--------------------------欢迎进入游戏世界--------------------------\n"); System.out.println("\t\t********************************"); System.out.println("\t\t**********猜拳,开始*************"); System.out.println("\t\t********************************"); System.out.println(); System.out.println("出拳规则:1.剪刀 2.石头 3.布"); System.out.print("请选择对方角色(1:刘备2:孙权3:曹操):"); boolean b = true; do { number = input.nextInt(); if (number == 1 || number == 2 || number == 3) { switch (number) { case 1: is.name = "刘备"; break; case 2: is.name = "孙权"; break; case 3: is.name = "曹操"; break; } b = false; } else { System.out.println("输入数字有误,请重新输入"); } } while (b); System.out.print("请输入你的姓名:"); it.name = input.next(); System.out.println(it.name + " VS " + is.name + " 对战\n"); System.out.println("要开始吗?(y/n)"); char answer = input.next().charAt(0); while (answer == ‘y‘) { it.show(); is.show(); if (it.num == is.sum) { System.out.println("和局"); } else if (((it.num == 2) && (is.sum == 1)) || (it.num == 1) && (is.sum == 3) || ((it.num == 3) && (is.sum == 2))) { System.out.println("你赢了,真棒!"); it.score++; } else { System.out.println("你输了,真笨!"); is.score++; } witch++; System.out.println("\n"); System.out.println("还要继续吗?(y/n)"); answer = input.next().charAt(0); } } /** * 定义游戏结算的方法 */ public void showResult() { System.out.println("********************************"); System.out.println(it.name + " VS " + is.name); System.out.println("对战次数:" + witch); System.out.println("\n姓名\t\t得分"); System.out.println(it.name + "\t\t" + it.score); System.out.println(is.name + "\t\t" + is.score); if (it.score < is.score) { System.out.println("你真笨,下次加油!"); } else if (it.score == is.score) { System.out.println("竟然平局,我们下次一决胜负!"); } else { System.out.println("你真棒!太赞了!!"); } System.out.println("********************************"); } }
package java12; public class renjicaiquan3 { public static void main(String[] args) { renjicaiquan2 play=new renjicaiquan2();//创建游戏类的对象 play.process();//调用游戏类的游戏进程方法 play.showResult();//调用游戏类的结算方法 } }
action = "剪刀"; break; case 2: action = "石头"; break; case 3: action = "布"; break; } a = false; System.out.println("你出拳:" + action); } else { System.out.println("输入数字有误,请重新输入"); }
标签:OLE case 数字 scanner int 定义 电脑 世界 dom
原文地址:https://www.cnblogs.com/lowerma/p/9789175.html