面向对象基础-例子

 1 package com.caiquan.pojo;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Person {
 6     String name="小明";
 7     int score;
 8     
 9     public int getScore() {
10         return score;
11     }
12     
13     public void setScore(int score) {
14         this.score = score;
15     }
16     
17     public int chuquan() {
18         Scanner in=new Scanner(System.in);
19         System.out.println("您请出拳:1、石头,2、剪刀,3、布");
20         int result=in.nextInt();
21         
22         if(result==1) {
23             System.out.println("您出的是石头");
24         }else if(result==2) {
25             System.out.println("您出的是剪刀");
26         }else if(result==3) {
27             System.out.println("您出的是布");
28         }else{
29             System.out.println("您出拳错误");
30         }
31         
32         return result;
33         
34     }
35 
36 }

 

 1 package com.caiquan.pojo;
 2 
 3 import java.util.Random;
 4 
 5 public class Computer {
 6     String name="小智";
 7     int score;
 8     
 9     public int getScore() {
10         return score;
11     }
12     
13     public void setScore(int score) {
14         this.score = score;
15     }
16     
17     public int chuquan() {
18         Random r=new Random();
19         int result=r.nextInt(3)+1;
20         
21         if(result==1) {
22             System.out.println("小智出的是石头");
23         }else if(result==2) {
24             System.out.println("小智出的是剪刀");
25         }else if(result==3) {
26             System.out.println("小智出的是布");
27         }else{
28             System.out.println("小智出拳错误");
29         }
30         
31         return result;
32         
33         
34     }
35 }

 

 1 package com.caiquan.pojo;
 2 
 3 public class Game {
 4     Person person=new Person();
 5     Computer computer=new Computer();
 6     int count=0;
 7     
 8     //1、石头,2、剪刀,3、布
 9     public void startGame() {
10         System.out.println("游戏开始");
11         System.out.println(person.name+"-----vs-----"+computer.name);
12         
13         for(int i=1;i<=5;i++) {
14             count=i;
15             System.out.println("当前开始的是第"+count+"轮");
16             int pchoice=person.chuquan();
17             int cchoice=computer.chuquan();
18             if((pchoice==1 && cchoice==2) || (pchoice==2 && cchoice==3) || (pchoice==3 && cchoice==1)) {
19                 System.out.println("恭喜您赢了");
20                 this.person.score+=1;
21             }else if((pchoice==2 && cchoice==1) || (pchoice==3 && cchoice==2) || (pchoice==1 && cchoice==3)) {
22                 System.out.println("对不起,您输了");
23                 this.computer.score+=1;
24             }else if(pchoice==0) {
25                 this.computer.score+=1;
26             }else if(cchoice==0) {
27                 this.person.score+=1;
28             }
29             
30             getRsult();
31         }
32         
33         
34     }
35     
36     public void getRsult() {
37         System.out.println("当前玩家分数:"+this.person.getScore()+",小智分数:"+this.computer.getScore());
38         if(this.person.getScore()>this.computer.getScore()) {
39             System.out.println("玩家胜!");
40         }else if(this.person.getScore()<this.computer.getScore()) {
41             System.out.println("小智胜!");
42         }
43         System.out.println("game over!");
44     }
45     
46 
47 }

 

 1 package com.caiquan.test;
 2 
 3 import com.caiquan.pojo.Game;
 4 
 5 public class Test1{
 6 
 7     public static void main(String[] args) {
 8         Game game=new Game();
 9         game.startGame();
10 
11     }
12 
13 }

 

posted on 2020-06-16 20:41  cherry_ning  阅读(50)  评论(0)    收藏  举报

导航