引用老師 Release 的 Source code 稍微改造一下,並且只使用目前教過的東西。
花了今天一些時間到補習班實作,僅供參考@@"
package guessgame;
import javax.swing.*;
public class Guess {
public static void main(String[] args) { int a = 0, b = 0, c = 0; a = (int) (Math.random() * 9 + 1); b = (int) (Math.random() * 9 + 1); for (; a == b;) { b = (int) (Math.random() * 9 + 1); } c = (int) (Math.random() * 9 + 1); for (; c == b || c == a;) { c = (int) (Math.random() * 9 + 1); }
String userInput = JOptionPane.showInputDialog("請輸入三個數字");
int playerInput = Integer.parseInt(userInput); //先做數字個數判斷,確保個數是3個,才不會造成變數 d、e、f 出錯。 for (; playerInput < 100 || playerInput > 1000;) { for (; playerInput < 100;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數少於三個"); userInput = JOptionPane.showInputDialog("請再輸入一次"); playerInput = Integer.parseInt(userInput); } for (; playerInput > 1000;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數多於三個"); userInput = JOptionPane.showInputDialog("請再輸入一次"); playerInput = Integer.parseInt(userInput); } }
int d = playerInput / 100; int e = (playerInput % 100) / 10; int f = playerInput % 10; /* 第一層loop是set所有特殊情況,任一發生即進入。 * 因判斷重複的loop還會再讓Player輸入一次,所以還是需要判斷個數的loop。 * 第二層loop針對所有特殊情況分類處理。 * 只用在第一次輸入時,判斷Player輸入的數字少於三個、多於三個、重複。 * * 缺點: * 輸入同樣數字,會因前一次掉入特殊情況的loop,造成顯示不一樣的對話方塊顯示,例如: * 輸入1234會判斷個數多於三個,接著輸入1122,一樣會判斷多於三個; * 若改先輸入112會判斷重複,接著輸入1122,一樣會判斷重複。 * 結論是同樣輸入1122,卻會因為前一次的輸入而jump不一樣的對話框, * 不過loop顯示出來的對話框訊息,勉強算是正確就是了。 */ for (; playerInput < 100 || playerInput > 1000 || d == e || d == f || e == f;) { for (; playerInput < 100;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數少於三個"); userInput = JOptionPane.showInputDialog("請再輸入一次"); playerInput = Integer.parseInt(userInput); } for (; playerInput > 1000;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數多於三個"); userInput = JOptionPane.showInputDialog("請再輸入一次"); playerInput = Integer.parseInt(userInput); } for (; d == e || d == f || e == f;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入了重複的數字"); userInput = JOptionPane.showInputDialog("請再輸入一次"); playerInput = Integer.parseInt(userInput); d = playerInput / 100; e = (playerInput % 100) / 10; f = playerInput % 10; } d = playerInput / 100; e = (playerInput % 100) / 10; f = playerInput % 10; }
//第二次以後要提示之前猜的AB數,所以用while做第二次以後的loop,直到猜中就break。 for (; true;) { int scoreA = 0, scoreB = 0; if (a == d) { scoreA++; } if (b == e) { scoreA++; } if (c == f) { scoreA++; } if (a == e || a == f) { scoreB++; } if (b == d || b == f) { scoreB++; } if (c == d || c == e) { scoreB++; }
//當數字及位置沒有全對時進入。 if (scoreA != 3) { userInput = JOptionPane.showInputDialog(scoreA + " A\n" + scoreB + " B\n請再輸入三個數字"); playerInput = Integer.parseInt(userInput); //先做數字個數判斷,確保個數是3個,才不會造成變數 d、e、f 出錯。 for (; playerInput < 100 || playerInput > 1000;) { for (; playerInput < 100;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數少於三個"); userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B"); playerInput = Integer.parseInt(userInput); } for (; playerInput > 1000;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數多於三個"); userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B"); playerInput = Integer.parseInt(userInput); } } d = playerInput / 100; e = (playerInput % 100) / 10; f = playerInput % 10; //同上面的迴圈,只是多加了提醒Player目前是幾A幾B的訊息。 for (; playerInput < 100 || playerInput > 1000 || d == e || d == f || e == f;) { for (; userInput.length() < 3;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數少於三個"); userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B"); playerInput = Integer.parseInt(userInput); } for (; userInput.length() > 3;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入的數字個數多於三個"); userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B"); playerInput = Integer.parseInt(userInput); } for (; d == e || d == f || e == f;) { JOptionPane.showMessageDialog(null, "錯誤!您輸入了重複的數字"); userInput = JOptionPane.showInputDialog("請再輸入一次\n" + scoreA + " A\n" + scoreB + " B"); playerInput = Integer.parseInt(userInput); d = playerInput / 100; e = (playerInput % 100) / 10; f = playerInput % 10; } d = playerInput / 100; e = (playerInput % 100) / 10; f = playerInput % 10; } } else { JOptionPane.showMessageDialog(null, "您猜對啦:)遊戲結束!"); break; } } } }
|
沒有留言:
張貼留言