一、難點突破
1 知識梳理
二、綜合練習
1 階段1:練習——分析業務,創建用戶類
1.1 需求說明
分析業務,抽象出類、類的特征和行為
創建用戶類

1.2 分析


1.3 代碼
-
創建用戶:定義用戶類Person,定義類的屬性(name、score)和類的方法(showFirst())
/**
*
* @author wangshaohua
*
*/
public class Person {
String name = "匿名"; // 名字
int score = 0; // 積分
/**
* 出拳
*
* @return 出拳結果:1.剪刀 2.石頭 3.布
*/
public int showFist() {
// 接收用戶的選擇
Scanner input = new Scanner(System.in);
System.out.print("\n請出拳:1.剪刀 2.石頭 3.布 (輸入相應數字) :");
int show = input.nextInt();
// 輸出出拳結果,並返回
switch (show) {
case 1:
System.out.println("你出拳: 剪刀");
break;
case 2:
System.out.println("你出拳: 石頭");
break;
case 3:
System.out.println("你出拳: 布");
break;
}
return show;
}
}
/**
*
* @author wangshaohua
*
*/
public class Person {
String name = "匿名"; // 名字
int score = 0; // 積分
/**
* 出拳
*
* @return 出拳結果:1.剪刀 2.石頭 3.布
*/
public int showFist() {
// 接收用戶的選擇
Scanner input = new Scanner(System.in);
System.out.print("\n請出拳:1.剪刀 2.石頭 3.布 (輸入相應數字) :");
int show = input.nextInt();
// 輸出出拳結果,並返回
switch (show) {
case 1:
System.out.println("你出拳: 剪刀");
break;
case 2:
System.out.println("你出拳: 石頭");
break;
case 3:
System.out.println("你出拳: 布");
break;
}
return show;
}
}
- 測試用戶出拳
/**
* 人機互動版猜拳游戲
* 階段1:測試用戶出拳
*
*/
public class TestPerson {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.showFist());
}
}
2 階段2:練習——創建計算機類
2.1 需求說明
創建計算機類Computer。實現計算機出拳

2.2 分析
產生一個1~3的隨機數,模擬計算機的出拳結果,例如,產生2,顯示“電腦出拳:石頭”
2.3 代碼
- 計算機類
/**
* 計算機類
* 階段2完成
*/
public class Computer {
String name = "電腦"; // 名字
int score = 0;; // 積分
/**
* 出拳
* @return 出拳結果:1.剪刀 2.石頭 3.布
*/
public int showFist(){
// 產生隨機數
int show = (int)(Math.random()*10)%3 + 1; //產生隨機數,表示電腦出拳
// 輸出出拳結果並返回
switch(show){
case 1:
System.out.println(name+"出拳: 剪刀");
break;
case 2:
System.out.println(name+"出拳: 石頭");
break;
case 3:
System.out.println(name+"出拳: 布");
break;
}
return show;
}
}
- 測試計算機類
/**
* 人機互動版猜拳游戲
* 階段2:測試電腦出拳
*/
public class TestComputer {
public static void main(String[] args) {
Computer computer = new Computer();
System.out.println(computer.showFist());
}
}
3 階段3 練習——創建游戲類,選擇對戰對手
3.1 需求說明
- 創建游戲類Game
- 編寫游戲類的初始化方法initial()
- 編寫游戲類的開始游戲方法startGame()
3.2 分析
3.3 代碼
- 游戲類
/**
* 游戲類
*/
public class Game1 {
Person person; //甲方
Computer computer; //乙方
int count; //對戰次數
/**
* 初始化
*/
public void initial(){
person = new Person();
computer = new Computer();
count = 0;
}
/**
* 開始游戲
*/
public void startGame() {
initial();
System.out.println("----------------歡 迎 進 入 游 戲 世 界----------------\n");
System.out.println("\n\t\t******************");
System.out.println ("\t\t** 猜拳, 開始 **");
System.out.println ("\t\t******************");
System.out.println("\n\n出拳規則:1.剪刀 2.石頭 3.布");
/*選擇對方角色*/
System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
Scanner input = new Scanner(System.in);
int role = input.nextInt();
if(role == 1){
computer.name = "劉備";
}else if(role == 2){
computer.name = "孫權";
}else if(role == 3){
computer.name = "曹操";
}
System.out.print("你選擇了"+computer.name+"對戰");
}
}
- 測試開始游戲:選擇對戰角色
/**
* 人機互動版猜拳游戲
* 階段3:測試開始游戲:選擇對戰角色
*/
public class TestGame1 {
public static void main(String[] args) {
Game1 game = new Game1();
game.startGame();
}
}
4 階段4:練習——實現一局對戰
4.1 需求說明
分別調用用戶類和計算機類的出拳方法showFist(),接受返回值並比較,給出勝負結果

4.2 分析
4.3 代碼
- 實現一局對戰
/**
* 游戲類
* 階段4:實現一局對戰
*/
public class Game2 {
Person person; //甲方
Computer computer; //乙方
int count; //對戰次數
/**
* 初始化
*/
public void initial(){
person = new Person();
computer = new Computer();
count = 0;
}
/**
* 開始游戲
*/
public void startGame() {
initial(); // 初始化
System.out.println("----------------歡 迎 進 入 游 戲 世 界----------------\n");
System.out.println("\n\t\t******************");
System.out.println ("\t\t** 猜拳, 開始 **");
System.out.println ("\t\t******************");
System.out.println("\n\n出拳規則:1.剪刀 2.石頭 3.布");
/*選擇對方角色*/
System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
Scanner input = new Scanner(System.in);
int role = input.nextInt();
if(role == 1){
computer.name = "劉備";
}else if(role == 2){
computer.name = "孫權";
}else if(role == 3){
computer.name = "曹操";
}
System.out.println("你選擇了 "+computer.name+"對戰");
/*開始游戲*/
System.out.print("\n要開始嗎?(y/n) ");
String con = input.next();
int perFist; //用戶出的拳
int compFist; //計算機出的拳
if(con.equals("y")){
/*出拳*/
perFist = person.showFist();
compFist = computer.showFist();
/*裁決*/
if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
System.out.println("結果:和局,真衰!\n"); //平局
}else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
System.out.println("結果: 恭喜, 你贏了!"); //用戶贏
}else{
System.out.println("結果說:^_^,你輸了,真笨!\n"); //計算機贏
}
}
}
}
- 測試開始游戲:實現1局對戰
/**
* 人機互動版猜拳游戲
* 階段4:測試開始游戲:實現1局對戰
*/
public class TestGame2 {
public static void main(String[] args) {
Game2 game = new Game2();
game.startGame();
}
}
5 階段5:練習——實現循環對戰,並累計得分
5.1 需求說明
實現循環對戰,
並且累加贏家的得分

5.2 分析
5.3 代碼
- 實現循環對戰
/**
* 游戲類
* 階段5:實現循環對戰
*
*/
public class Game3 {
Person person; //甲方
Computer computer; //乙方
int count; //對戰次數
/**
* 初始化
*/
public void initial(){
person = new Person();
computer = new Computer();
count = 0;
}
/**
* 開始游戲
*/
public void startGame() {
initial(); // 初始化
System.out.println("----------------歡 迎 進 入 游 戲 世 界----------------\n");
System.out.println("\n\t\t******************");
System.out.println ("\t\t** 猜拳, 開始 **");
System.out.println ("\t\t******************");
System.out.println("\n\n出拳規則:1.剪刀 2.石頭 3.布");
/*選擇對方角色*/
System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
Scanner input = new Scanner(System.in);
int role = input.nextInt();
if(role == 1){
computer.name = "劉備";
}else if(role == 2){
computer.name = "孫權";
}else if(role == 3){
computer.name = "曹操";
}
System.out.println("你選擇了 "+computer.name+"對戰");
/*開始游戲*/
System.out.print("\n要開始嗎?(y/n) ");
String con = input.next();
int perFist; //用戶出的拳
int compFist; //計算機出的拳
while(con.equals("y")){
/*出拳*/
perFist = person.showFist();
compFist = computer.showFist();
/*裁決*/
if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
System.out.println("結果:和局,真衰!嘿嘿,等着瞧吧 !\n"); //平局
}else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
System.out.println("結果: 恭喜, 你贏了!"); //用戶贏
person.score++;
}else{
System.out.println("結果說:^_^,你輸了,真笨!\n"); //計算機贏
computer.score++;
}
count++;
System.out.print("\n是否開始下一輪(y/n): ");
con = input.next();
}
}
}
- 測試循環對戰
public class TestGame3 {
public static void main(String[] args) {
Game3 game = new Game3();
game.startGame();
}
}
6 階段6:練習——顯示對戰結果
6.1 需求說明
游戲結束后,顯示對戰結果
6.2 分析
編寫showResult( )方法,
比較二者的得分情況,
給出對戰結果
6.3 代碼
- 實現對戰結果顯示
/**
* 游戲類
* 階段6:實現對戰結果顯示
*/
public class Game4 {
Person person; //甲方
Computer computer; //乙方
int count; //對戰次數
/**
* 初始化
*/
public void initial(){
person = new Person();
computer = new Computer();
count = 0;
}
/**
* 開始游戲
*/
public void startGame() {
initial(); // 初始化
System.out.println("----------------歡 迎 進 入 游 戲 世 界----------------\n");
System.out.println("\n\t\t******************");
System.out.println ("\t\t** 猜拳, 開始 **");
System.out.println ("\t\t******************");
System.out.println("\n\n出拳規則:1.剪刀 2.石頭 3.布");
/*選擇對方角色*/
System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
Scanner input = new Scanner(System.in);
int role = input.nextInt();
if(role == 1){
computer.name = "劉備";
}else if(role == 2){
computer.name = "孫權";
}else if(role == 3){
computer.name = "曹操";
}
System.out.println("你選擇了 "+computer.name+"對戰");
System.out.print("\n要開始嗎?(y/n) ");
String con = input.next();
int perFist; //用戶出的拳
int compFist; //計算機出的拳
while(con.equals("y")){
/*出拳*/
perFist = person.showFist();
compFist = computer.showFist();
/*裁決*/
if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
System.out.println("結果:和局,真衰!嘿嘿,等着瞧吧 !\n"); //平局
}else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
System.out.println("結果: 恭喜, 你贏了!"); //用戶贏
person.score++;
}else{
System.out.println("結果說:^_^,你輸了,真笨!\n"); //計算機贏
computer.score++;
}
count++;
System.out.print("\n是否開始下一輪(y/n): ");
con = input.next();
}
/*顯示結果*/
showResult();
}
/**
* 顯示比賽結果
*/
public void showResult(){
/*顯示最后結果*/
System.out.println("---------------------------------------------------");
System.out.println(computer.name + " VS " + person.name);
System.out.println("對戰次數:"+ count);
int result = calcResult();
if(result == 1){
System.out.println("結果:打成平手,下次再和你一分高下!");
}else if(result == 2){
System.out.println("結果:恭喜恭喜!"); //用戶獲勝
}else{
System.out.println("結果:呵呵,笨笨,下次加油啊!"); //計算機獲勝
}
System.out.println("---------------------------------------------------");
}
/**
* 計算比賽結果
* @return 1:戰平;2:用戶贏;3:電腦贏
*/
public int calcResult(){
if(person.score == computer.score){
return 1; // 戰平
}else if(person.score > computer.score){
return 2; // 用戶贏
}else{
return 3; // 電腦贏
}
}
}
- 測試
public class TestGame4 {
/**
* 人機互動版猜拳游戲
*/
public static void main(String[] args) {
Game4 game = new Game4();
game.initial();
game.startGame();
}
}
7 階段7:練習——完善游戲類的startGame()
7.1 需求說明
輸入並保存用戶姓名,游戲結束后顯示雙方的各自得分
7.2 分析
7.3 代碼
- 功能擴展
public class Game {
Person person; //甲方
Computer computer; //乙方
int count; //對戰次數
/**
* 初始化
*/
public void initial(){
person = new Person();
computer = new Computer();
count = 0;
}
/**
* 開始游戲
*/
public void startGame() {
System.out.println("----------------歡 迎 進 入 游 戲 世 界----------------");
System.out.println("\n\t\t******************");
System.out.println ("\t\t** 猜拳, 開始 **");
System.out.println ("\t\t******************");
System.out.println("\n出拳規則:1.剪刀 2.石頭 3.布");
Scanner input = new Scanner(System.in);
String exit = "n"; // 退出系統
do{
initial(); // 初始化
/*選擇對方角色*/
System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
int role = input.nextInt();
if(role == 1){
computer.name = "劉備";
}else if(role == 2){
computer.name = "孫權";
}else if(role == 3){
computer.name = "曹操";
}
// 擴展功能1:輸入用戶姓名
/*輸入用戶姓名*/
System.out.print("請輸入你的姓名:");
person.name = input.next();
System.out.println(person.name+" VS "+computer.name+" 對戰\n");
// 擴展功能1結束
System.out.print("要開始嗎?(y/n) ");
String start = input.next(); // 開始每一局游戲
int perFist; //用戶出的拳
int compFist; //計算機出的拳
while(start.equals("y")){
/*出拳*/
perFist = person.showFist();
compFist = computer.showFist();
/*裁決*/
if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
System.out.println("結果:和局,真衰!嘿嘿,等着瞧吧 !\n"); //平局
}else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
System.out.println("結果: 恭喜, 你贏了!"); //用戶贏
person.score++;
}else{
System.out.println("結果說:^_^,你輸了,真笨!\n"); //計算機贏
computer.score++;
}
count++;
System.out.print("\n是否開始下一輪(y/n): ");
start = input.next();
}
/*顯示結果*/
showResult();
// 擴展功能3:循環游戲,直到退出系統
System.out.print("\n要開始下一局嗎?(y/n):");
exit = input.next();
System.out.println();
// 擴展功能3結束
}while(!exit.equals("n"));
System.out.println("系統退出!");
}
/**
* 顯示比賽結果
*/
public void showResult(){
/*顯示對戰次數*/
System.out.println("---------------------------------------------------");
System.out.println(computer.name + " VS " + person.name);
System.out.println("對戰次數:"+ count);
// 擴展功能2:顯示最終的得分
System.out.println("\n姓名\t得分");
System.out.println(person.name+"\t"+person.score);
System.out.println(computer.name+"\t"+computer.score+"\n");
// 擴展功能2結束
/*顯示對戰結果*/
int result = calcResult();
if(result == 1){
System.out.println("結果:打成平手,下次再和你一分高下!");
}else if(result == 2){
System.out.println("結果:恭喜恭喜!"); //用戶獲勝
}else{
System.out.println("結果:呵呵,笨笨,下次加油啊!"); //計算機獲勝
}
System.out.println("---------------------------------------------------");
}
/**
* 計算比賽結果
* @return 1:戰平;2:用戶贏;3:電腦贏
*/
public int calcResult(){
if(person.score == computer.score){
return 1; // 戰平
}else if(person.score > computer.score){
return 2; // 用戶贏
}else{
return 3; // 電腦贏
}
}
}
測試
/**
* 人機互動版猜拳游戲
* 程序入口
*/
public class StartGuess {
public static void main(String[] args) {
Game game = new Game();
game.startGame();
}
}
三、總結
- 類和對象的關系是抽象和具體的關系
- 使用類的步驟如下
- (1)定義類:使用關鍵字class。
- (2)創建類的對象:使用關鍵字new。
- (3)使用類的屬性和方法:使用“.”操作符。
- 定義類的方法包括三個部分
- (1)方法的名稱
- (2)方法返回值的類型
- (3)方法的主體
- 類的方法調用,使用如下兩種形式。
- (1)同一個類中的方法,直接使用方法名
- (2)不同類的方法,先創建對象,再使用“對象名.方法名”
- 在Java中,有成員變量和局部變量,它們的作用域各不相同
關注我們

捐贈我們
良師益友工作室一直在致力於幫助編程愛好更加快速方便地學習編程,如果您對我們的成果表示認同並且覺得對你有所幫助,歡迎您對我們捐贈^_^。
