在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

猜拳游戏

2016-7-29 15:47| 发布者: zhangjf| 查看: 711| 评论: 0

摘要: 简单的小游戏 package com.zryy.gussgame;import java.util.Scanner;public class Testing { public static void main(String args) { int choice = 0; int number = 0 ...
简单的小游戏
                   
                                            
  1. package com.zryy.gussgame;
  2. import java.util.Scanner;
  3. public class Testing {
  4.         public static void main(String[] args) {
  5.                 int choice = 0;
  6.                 int number = 0;
  7.                 String result = null;
  8.                 Scanner input = new Scanner(System.in);
  9.                 String start;
  10.                 Person person = new Person();
  11.                 Computer compu = new Computer();
  12.                 Game gussrule = new Game();
  13.                 boolean flag = true;
  14.                 System.out.println("************" + "欢迎进入游戏世界*************");
  15.                 System.out.println("             *************");
  16.                 System.out.println("                             猜拳开始");
  17.                 System.out.println("             **************");
  18.                 System.out.println();
  19.                 System.out.println();
  20.                 System.out.println();
  21.                 System.out.println("出拳规则:1.剪刀、2.石头、3.布");
  22.                 System.out.println();
  23.                 System.out.println();
  24.                 System.out.println();
  25.                 System.out.println("要开始么?请选择y/n");
  26.                 start = input.next();
  27.                 System.out.println("*************************************************");
  28.                 while ("y".equals(start)) {
  29.                         person.Personin();
  30.                         System.out.print("要开始么?请输入y/n");
  31.                         start = input.next();
  32.                         System.out.println("**************************************************");
  33.                         if ("y".equals(start)) {
  34.                                 person.Showperson(choice);
  35.                                 gussrule.gamerule(result);
  36.                                 break;
  37.                         } else {
  38.                                 System.out.println("结束程序");
  39.                                 break;
  40.                         }
  41.                 }
  42.         }
  43. }
复制代码
                                                                                                               
                                       
                                                                               
                                            
  1. package com.zryy.gussgame;
  2. import java.util.Scanner;
  3. public class Person {
  4.         Scanner input = new Scanner(System.in);
  5.         String name;
  6.         int namerole;
  7.         public void Personin() {
  8.                 boolean flag = true;
  9.                 System.out.println("????????????" + "1.????" + " " + "2.???" + " " + "3.???" + "??????????");
  10.                 namerole = input.nextInt();
  11.                 System.out.println("????????????");
  12.                 name = input.next();
  13.                 switch (namerole) {
  14.                 case 1:
  15.                         System.out.println("????" + " VS " + "name");
  16.                         flag = false;
  17.                         break;
  18.                 case 2:
  19.                         System.out.println("???" + " VS " + "name");
  20.                         flag = false;
  21.                         break;
  22.                 case 3:
  23.                         System.out.println("???" + " VS " + "name");
  24.                         flag = false;
  25.                         break;
  26.                 default:
  27.                         System.out.println("?????????????????");
  28.                         flag = true;
  29.                 }
  30.         }
  31.         public int Showperson(int choice) {
  32.                 Computer com = new Computer();
  33.                 String result = null;
  34.                 Game gussrule = new Game();
  35.                 System.out.print("1.????" + "  " + "2.??" + "  " + "3.??" + "(???????????????):");
  36.                 choice = input.nextInt();
  37.                 System.out.println("***************************************************");
  38.                 switch (choice) {
  39.                 case 1:
  40.                         System.out.println("??????????");
  41.                         break;
  42.                 case 2:
  43.                         System.out.println("????????");
  44.                         break;
  45.                 case 3:
  46.                         System.out.println("????????");
  47.                         break;
  48.                 default:
  49.                         System.out.println("?????????????????");
  50.                 }
  51.                 return choice;
  52.         }
  53. }
复制代码
                                                                                                               
                                       
                                                                               
                                            
  1. package com.zryy.gussgame;
  2. import java.util.Scanner;
  3. public class Game {
  4.         public String gamerule(String result){
  5.         Scanner input=new Scanner(System.in);
  6.           int choice = 0;
  7.           int number = 0;
  8.                 boolean flag=true;
  9.                 Person persons=new Person();
  10.                 Computer computers = new Computer();
  11.                 int a;
  12.                 int b;
  13.                 int scorep=0;
  14.                 int scorec=0;
  15.                 int countp=0;
  16.                 int countc=0;
  17.                 while(flag){
  18.                  a=persons.Showperson(choice);
  19.                  b=computers.ShowComp(number);
  20.                
  21.                  
  22.                
  23.                         if(a==b){
  24.                                 System.out.println("平局");
  25.                                 scorec++;
  26.                                 scorep++;
  27.                                 System.out.println("是否继续?y/n");
  28.                                 String goon=input.next();
  29.                                                 if("y".equals(goon)){
  30.                                                        
  31.                                                         flag=true;
  32.                                                 }
  33.                                                 else{
  34.                                                         flag=false;
  35.                                                 }
  36.                                
  37.                                
  38.                                
  39.                         }
  40.                         else if((a==1&&b==2)||(a==2&&b==3)||(a==3&&b==1)){
  41.                                 scorec+=3;
  42.                                 System.out.println("电脑胜利");
  43.                                 System.out.print("是否继续?y/n");
  44.                                 String goon=input.next();
  45.                                                 if("y".equals(goon)){
  46.                                                         flag=true;
  47.                                                 }
  48.                                                 else{
  49.                                                         flag=false;
  50.                                                 }
  51.                         }
  52.                         else{
  53.                                 scorep+=3;
  54.                                 System.out.println("我胜利");
  55.                                 System.out.print("是否继续?y/n");
  56.                                 String goon=input.next();
  57.                                                 if("y".equals(goon)){
  58.                                                        
  59.                                                 }
  60.                                                 else{
  61.                                                         flag=false;
  62.                                                 }
  63.                         }
  64.                         countp+=scorep;
  65.                     countc+=scorec;
  66.                 }
  67.             System.out.println("我的得分"+scorep);
  68.             System.out.println("电脑得分"+scorec);
  69.                 System.out.println("程序结束");
  70.                 return result;
  71.         }
  72. }
复制代码
                                                                                                               
                                       
                                                                               
                                            
  1. package com.zryy.gussgame;
  2. import java.util.Scanner;
  3. public class Computer {
  4.        
  5.         Scanner input=new Scanner(System.in);
  6.         public int ShowComp(int number){
  7.                
  8.                
  9.                 number=(int) ((Math.random())*10%3+1);
  10.        
  11.                 boolean flag=true;
  12.             String name = null;
  13.                 while(flag){
  14.                         switch(number){
  15.                         case 1:
  16.                                
  17.                                 name ="剪刀";
  18.                                 break;
  19.                         case 2:
  20.                                
  21.                                 name ="石头";
  22.                                 break;
  23.                         case 3:
  24.                                
  25.                                 name ="布";
  26.                                 break;
  27.                         }
  28.                                 flag=false;
  29.                
  30.                 }
  31.                 System.out.println("电脑出拳为:"+name);
  32.            return number;
  33.         }
  34. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 15:39

Copyright 2015-2025 djqfx

返回顶部