多语言展示
当前在线:1367今日阅读:23今日分享:25

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

用java编写的一个人机交互,石头剪刀布小程序
方法/步骤
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); }}

注意事项

大家可以在下方的评论区评论留言,如有错误,请批评指正。

推荐信息