用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);
}
}
注重事项
- 大师可以鄙人方的评论区评论留言,若有错误,请攻讦斧正。
来源:百闻(微信/QQ号:9397569),转载请保留出处和链接!
本文链接:https://www.ibaiwen.com/web/229300.html
- 上一篇: win7怎么设置任务栏透明
- 下一篇: java.net.BindException异常解决方法
- 热门文章
-
WB蒙特利尔(WB Montreal)——欧美十大最差视频游戏开发商
迅猛龙(Velociraptor)——欧美史前十大死亡动物
什么是果酱猫(What Marmalade Cats)?
神奇蜘蛛侠2(The Amazing Spider-Man 2)——欧美最佳蜘蛛侠电影
希瑟(Heather)——欧美十大最佳柯南灰歌
二人梭哈
faceu激萌怎么把瘦脸开到最大
奥兹奥斯本(Ozzy Osbourne)——欧美十大高估歌手
什么是小脑前下动脉(Anterior Inferior Cerebellar Artery)?
我应该知道康涅狄格州的什么(What Should I Know About Connecticut)?
- 热评文章
- 最新评论
-
- 最近访客
-
- 站点信息
-
- 文章总数:200248
- 页面总数:9
- 分类总数:1
- 标签总数:0
- 评论总数:0
- 浏览总数:497