Java之石頭剪刀布
阿新 • • 發佈:2018-12-21
剪刀石頭布的遊戲 (1)定義一個計算機玩家類Computer,它有一個成員變數(name)和一個成員方法(show)。其中的成員變量表示它的名稱,成員方法表示出拳的行為,出的拳值是由系統產生隨機數表示。 (2)定義一個遊戲玩家類Player,它有一個成員變數(name)和一個成員方法(show)。其中的成員變量表示它的名稱,成員方法表示出拳的行為,出的拳值是由使用者輸入的整數表示。 (3)定義一個遊戲類Game,它有兩個成員變數(computer\player)和一個成員方法(start),其中的成員變數分別表示兩個對手,一個是計算機玩家物件,一個是遊戲玩家物件。成員方法表示一次遊戲過程。 (4)編寫一個測試類,完成一次剪刀石頭布的遊戲。
Top one:
import java.util.Scanner;import java.util.Random;public class example{ public static void main(String []args) { Computer c =new Computer("computer"); Player p = new Player("player"); Game g = new Game(c,p); g.start(); }}class Computer{ String name; public Computer(String name) { this.name = name; } int show() { int number=(int)(Math.random()*10) % 3; return number; }}class Player{ String name; int shu; public Player(String name) { this.name = name; } int show() { int number; number = 0 ; System.out.println("0代表石頭,1代表剪刀,2代表布"); System.out.println("請輸入一個數:"); Scanner sc = new Scanner(System.in); int shu = sc.nextInt(); System.out.println("玩家輸入的是:"+shu); System.out.println("計算機輸入的為:"+number); return shu; }}class Game{ Computer computer; Player player; public Game(Computer computer,Player player) { this.computer = computer; this.player = player; } void start() { int c_show = computer.show(); int p_show = player.show(); if(c_show == 0&& p_show ==1||c_show == 1&&p_show ==2||c_show ==2&&p_show ==0) { System.out.println("計算機贏了!"); } else if(c_show == p_show) System.out.println("平局"); else System.out.println("玩家贏了!"); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
執行結果如下:
Top two: 使用抽象類程式碼如下:
import java.util.Scanner;import java.util.Random;public class example{ public static void main(String []args) { ComputerPlayer c =new ComputerPlayer("computer"); PersonPlayer p = new PersonPlayer("player"); Game g = new Game(c,p); g.start(); }}//定義抽象類Player,有其特定的格式abstract class Player{ String name; abstract int show();}class ComputerPlayer extends Player{ String name; public ComputerPlayer(String name) { this.name = name; } int show() { int number=(int)(Math.random()*10) % 3; return number; }}class PersonPlayer extends Player{ String name; int shu; public PersonPlayer(String name) { this.name = name; } int show() { int number; number = 0; System.out.println("0代表石頭,1代表剪刀,2代表布"); System.out.println("請輸入一個數:"); Scanner sc = new Scanner(System.in); int shu = sc.nextInt(); System.out.println("玩家輸入的是:"+shu); System.out.println("計算機輸入的為:"+number); return shu; }}class Game{ ComputerPlayer computer; PersonPlayer player; public Game(ComputerPlayer computer,PersonPlayer player) { this.computer = computer; this.player = player; } void start() { int c_show = computer.show(); int p_show = player.show(); if(c_show == 0&& p_show ==1||c_show == 1&&p_show ==2||c_show ==2&&p_show ==0) { System.out.println("計算機贏了!"); } else if(c_show == p_show) System.out.println("平局"); else System.out.println("玩家贏了!"); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
Top three: 使用介面進行操作程式碼如下:
import java.util.Scanner;import java.util.Random;public class example{ public static void main(String []args) { Computer c =new Computer("computer"); Player p = new Player("player"); Game g = new Game(c,p); g.start(); }}class Computer{ String name; public Computer(String name) { this.name = name; } int show() { int number=((int)(Math.random()*10))%3; return number; }}class Player{ String name; int shu; public Player(String name) { this.name = name; } int show() { int number=((int)(Math.random()*10))%3; System.out.println("0代表石頭,1代表剪刀,2代表布"); System.out.println("請輸入一個數:"); Scanner sc = new Scanner(System.in); int shu = sc.nextInt(); System.out.println("玩家輸入的是:"+shu); System.out.println("計算機輸入的是:"+number); return shu; }}class Game{ Computer computer; Player player; public Game(Computer computer,Player player) { this.computer = computer; this.player = player; } void start() { int c_show = computer.show(); int p_show = player.show(); if(c_show == 0&& p_show ==1||c_show == 1&&p_show ==2||c_show ==2&&p_show ==0) { System.out.println("計算機贏了!"); } else if(c_show == p_show) System.out.println("平局"); else System.out.println("玩家贏了!"); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
By lijia 2017.4.10