java实现简单猜拳小游戏

网友投稿 266 2022-09-13


java实现简单猜拳小游戏

本文实例为大家分享了java实现猜拳小游戏的具体代码,供大家参考,具体内容如下

User.java

import java.util.Scanner;

public class User {

String name;

int score;

public int showFist(){

System.out.println ("请出拳:1.剪刀\t2.石头\t3.布");

Scanner input=new Scanner ( System.in );

int choice=input.nextInt ();

if(choice==1){

System.out.println ("您出了剪刀");

}else if(choice==2){

System.out.println ("您出了石头");

}else if (choice==3){

System.out.println ("您出了布");

}else {

System.out.println ("输入有误!");

}

return choice;

}

}

Computer.java

public class Computer {

String name;

int score;

public int showFist () {

int choice = (int) (Math.random () * 3) + 1; //产生随机数

if (choice == 1) {

System.out.println (name +"出了剪刀" );

} else if (choice == 2) {

System.out.println (name +"出了石头" );

} else if (choice == 3) {

System.out.println (name +"您出了布" );

} else {

System.out.printlnCmIGb ( "输入有误!" );

}

return choice;

}

}

Game.java

import java.util.Scanner;

public class Game {

User user; //用户

Computer computer; //计算机

int count; //记录对战次数

//初始化:设置自己的名字 对手的名字,积分0

public void init(){

System.out.println ("请输入自己的名字");

Scanner input = new Scanner ( System.in );

String name = input.next ();

user=new User (); //对象初始化

user.name=name;

user.score=0;

System.out.println ("请选择你的对手:\n1.张三\t2.李四\t3.王五");

int choice = input.nextInt ();

computer=new Computer (); //对象初始化

computer.score=0;

switch (choice){

case 1:

computer.name="张三";

case 2:

computer.name="李四";

case 3:

computer.name="王五";

break;

default:

System.out.println ("输入有误!");

}

System.out.println ("你选择与TA对战:"+computer.name);

}

public void start() {

init ();

Scanner input=new Scanner ( System.in );

String isContinue = null;

do {

int userFist = user.showFist (); //人出拳

int chomputerFist = computer.showFist (); //计算机出拳

calcResult ( userFist, chomputerFist );

System.out.println ("是否继续?y(继续)/其他(结束)");

isContinue=input.next ();

}while ("y".equals ( isContinue ));

showResult (user,computer); //显示最终结果

}

//计算每一轮的结果

public void calcResult(int userFist,int computerFist){

//"1.剪刀\t2.石头\t3.布"

if((userFist==1&&computerFist==3)||(userFist==2&&computerFist==1)||(userFist==3&&computerFist==2)){

System.out.println ("您赢了");

user.score++;

http:// }else if((userFist==3&&computerFist==1)||(userFist==1&&computerFist==2)||(userFist==2&&computerFist==3)){

System.out.println ("您输了");

computer.score++;

}else {

System.out.println ("您平局");

}

}

//计算最终结果

public void showResult(User user,Computer computer){

System.out.println (user.name +"\t" +user.score );

System.out.println (computer.name +"\t" +computer.score);

if (user.score>computer.score){

System.out.println ("恭喜,获得了最终的胜利");

}else if (user.score

System.out.println ("很遗憾你输了");

}else {

System.out.println ("最终平局...");

}

}

public static void main ( String[] args ) {

Game game = new Game ();

game.start ();

}

}

测试结果显示

System.out.println ("很遗憾你输了");

}else {

System.out.println ("最终平局...");

}

}

public static void main ( String[] args ) {

Game game = new Game ();

game.start ();

}

}

测试结果显示


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:学习笔记(学习笔记的格式)
下一篇:网络工程师成长日记139-试两个月行不行(网络工程师成长日记网站)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~