一个非常简单的石头剪刀布程序

 时间:2024-10-14 01:52:57

1、在学会了使用java编写hello world程序之后,我们学习了类,对象,for循环结构,switch结构,以及while结构,如此,我们开始编写一个小程序,用于模拟猜拳。

一个非常简单的石头剪刀布程序

2、我们嚼但匙噻首先构造一个类对象,来模拟用户对象。话不多说,贴代码。package game;/*** 这个类主要包含游垡幢踔笠戏玩家* @author THY**/public class Player { String name; int morraType; int score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMorraType() { return morraType; } public void setMorraType(int morraType) { this.morraType = morraType; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Player(String name, int morraType, int score) { super(); this.name = name; this.morraType = morraType; this.score = score; } public Player() { super(); }}

一个非常简单的石头剪刀布程序

3、//switch的结构public static void printType(int g) { switch(g) { case 1: System.out.println("剪刀"); break; case 2: System.out.println("石头"); break; case 3: System.out.println("布"); break; } }

一个非常简单的石头剪刀布程序

4、//switch的结构switch(a) { case 1: computer.setName("张飞"); break; case 2: computer.setName("曹操"); break; case 3: computer.setName("徐庶"); break; default: System.out.println("用户未定义名称,现在定义机器名称为电脑"); computer.setName("电脑"); break; }

一个非常简单的石头剪刀布程序

5、//这是对类的对象的使用Player player=new Player(); Player computer=new Player(); System.out.println("出拳规则:1.剪刀,2.石头,3.布"); System.out.println("请选择用户角色:1.张飞,2.曹操,3徐庶"); Scanner s=new Scanner(System.in);

一个非常简单的石头剪刀布程序

6、//这里贴源代码package game;/*** 这个类主要包含游戏玩家* @author THY**/public class Player { String name; int morraType; int score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMorraType() { return morraType; } public void setMorraType(int morraType) { this.morraType = morraType; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Player(String name, int morraType, int score) { super(); this.name = name; this.morraType = morraType; this.score = score; } public Player() { super(); }}

7、package game;import java.util.Scanner;public class Morra { public static void printType(int g) { switch(g) { case 1: System.out.println("剪刀"); break; case 2: System.out.println("石头"); break; case 3: System.out.println("布"); break; } } public static void main(String[] args) { // TODO Auto-generated method stub Player player=new Player(); Player computer=new Player(); System.out.println("出拳规则:1.剪刀,2.石头,3.布"); System.out.println("请选择用户角色:1.张飞,2.曹操,3徐庶"); Scanner s=new Scanner(System.in); int a=s.nextInt(); switch(a) { case 1: computer.setName("张飞"); break; case 2: computer.setName("曹操"); break; case 3: computer.setName("徐庶"); break; default: System.out.println("用户未定义名称,现在定义机器名称为电脑"); computer.setName("电脑"); break; } System.out.println("请输入你的姓名:"); Scanner sc=new Scanner(System.in); player.setName(sc.nextLine()); System.out.println(""+player.getName()+" VS "+computer.getName()); boolean flag=false; do { System.out.println("请出拳,出拳规则:1.剪刀,2.石头,3.布"); Scanner ssc=new Scanner(System.in); int play=ssc.nextInt(); int compute=(int)(Math.random()*3+1); switch(play){ case 1: switch(compute) { case 1: break; case 2: computer.score++; break; case 3: player.score++; break; } break; case 2: switch(compute) { case 1: player.score++; break; case 2: break; case 3: computer.score++; break; } break; case 3: switch(compute) { case 1: computer.score++; break; case 2: player.score++; break; case 3: break; } break; default: System.out.println("未知数据,本场不积分"); break; } System.out.print("玩家出拳:"); printType(play); System.out.print("电脑出拳:"); printType(compute); System.out.println(player.getName()+":"+player.getScore()); System.out.println(computer.getName()+":"+computer.getScore()); System.out.println("是否继续?(Y/N)"); Scanner sss=new Scanner(System.in); String check=sss.nextLine(); if(check.equals("Y")||check.equals("y")) { flag=true; } else if(check.equals("N")||check.equals("n")) { flag=false; } else { System.out.println("异常输入,程序退出"); break; } System.out.println("-------------------------------------"); }while(flag); }}

一个非常简单的石头剪刀布程序
  • WebStorm怎样切换项目
  • Keil uVision4 51单片机按键关灯效果编程
  • java用方法比较两个数据是否相等。
  • 小鸡模拟器遇到在之前删除的游戏后无法下载问题
  • 在python中怎样查看tkinter版本?
  • 热门搜索
    trash是什么意思 李白是个什么样的人 鸦雀无声是什么意思 memory什么意思 7月14是什么节日 什么是妊娠纹 suit什么意思 重油是什么 opposite是什么意思 脚凉是什么原因