用java編寫的一小我機交互,石頭鉸剪布小法式
在學會了利用java編寫hello world法式之后,我們進修了類,對象,for輪回布局,switch布局,以及while布局,如斯,我們起頭編寫一個小法式,用于模擬豁拳。
我們起首機關一個類對象,來模擬用戶對象。話不多說,貼代碼。
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();
}
}
//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;
}
}
//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;
}
//這是對類的對象的利用
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);
//這里貼源代碼
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();
}
}
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);
}
}
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!