2012-12-14 33 views
2

我試圖解決這個任務爲我們的主題。我已經嘗試過了,但沒有使用JOptionPane,但它工作正常,但是當我編輯它時有一個對話框,它沒有奏效。第一個對話框應該包含一個標題和要輸入的選項,最後一個應該讓用戶退出程序。用戶輸入他/她的選擇後,應出現一個對話框,指示用戶的選擇和計算機的選擇。電腦的選擇應該是隨機的。如果用戶沒有輸入正確的選項,會出現一個對話框,告訴用戶輸入一個有效的選項。這是我到目前爲止:在Java中使用JOptionPane的岩石紙剪刀遊戲

package assignment; 

import java.util.Random; 
import java.util.Scanner; 
import javax.swing.*; 
public class RockPaperScissorGame 
{ 



public static void main(String[] args) 
{ 

    String inputStr; 
    String personPlay="";  
    String computerPlay="1,2,3";  
    int computerInt; 
    Scanner input = new Scanner(System.in); 
    Random generator = new Random(); 


      inputStr = JOptionPane.showInputDialog("Lets play a game! \nEnter 1 for rock \nEnter 2 for paper \nEnter 3 for scissors \nEnter 4 to quit"); 
      personPlay = input.next(inputStr); 

      switch (computerInt = 0) 
      { 
       } 
      do 
      { 
       if (personPlay.equals(computerPlay)) 
        JOptionPane.showMessageDialog(null, "It's a TIE! ", "TIE!", JOptionPane.INFORMATION_MESSAGE); 

       else if (personPlay.equals("1")) 
        { 
        if (computerPlay.equals("3")) 
        JOptionPane.showMessageDialog(null, "Rock beats Scissors. \nYOU WIN! ", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE); 
        else 
        JOptionPane.showMessageDialog(null, "Paper beats Rock. \nYOU LOSE! ", "YOU LOSE!", JOptionPane.INFORMATION_MESSAGE); 
        } 
       else if (personPlay.equals("2")) 
        { 
        if (computerPlay.equals("3")) 
        JOptionPane.showMessageDialog(null, "Scissor beats Paper. \nYOU LOSE!", "YOU LOSE!", JOptionPane.INFORMATION_MESSAGE); 
        else 
        JOptionPane.showMessageDialog(null, "Paper beats Rock. \nYOU WIN! ", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE); 
        } 
       else if (personPlay.equals("3")) 
        { 
        if (computerPlay.equals("1")) 
        JOptionPane.showMessageDialog(null, "Scissor beats Paper. \nYOU WIN!", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE); 
        else 
        JOptionPane.showMessageDialog(null, "Rock beats Scissors. \nYOU LOSE! ", "YOU LOSE!", JOptionPane.INFORMATION_MESSAGE); 
        } 
       else if (personPlay.equals("4")) 
        { 
        JOptionPane.showMessageDialog(null, "GOOD BYE", " BYE!", JOptionPane.INFORMATION_MESSAGE); 
        } 


      }while(true); 
      } 

} 

希望你們可以幫忙。謝謝:)

+2

是'computerPlay'從「1,2,3」改變過? 'personPlay'會等於「1,2,3」嗎? –

回答

2
  • 正如指出,你永遠不會改變computerPlay,這應該是隨機選擇。
  • showInputDialog應該在循環內,否則遊戲永遠不會結束...
  • switch是無用的。
  • 所以是Scanner。只是做:personPlay = JOptionPane.showInputDialog("Lets play a game! [...]");