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

2025-04-18 13:31:41

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); }}

一个非常简单的石头剪刀布程序
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢