簡體   English   中英

如何循環一個JOptionsPane

[英]How to loop a JOptionsPane

好的,我通過搜索進行了半找到,但是現在我遇到了另一個問題。當第二個框彈出並單擊“否”時,第一個框仍然運行,如果我取消它,則會出現錯誤。 我究竟做錯了什么?

  import javax.swing.*;
 import java.util.*;
import java.util.Scanner;

public class RPS {

public static void main(String[] args) {

    Scanner sc = new Scanner (System.in);
    String input;                      
    int user;                       
    int computer;                   
while (true){//here we go
        input = JOptionPane.showInputDialog("What'll it be? Rock, paper, or scissors?\n" +
                "1 for rock, 2 for paper, and 3 for scissors: ",JOptionPane.YES_NO_OPTION);


    user = Integer.parseInt(input);


    Random randomnum = new Random ();

    computer = randomnum.nextInt(3);




    if (user == 1 && computer == 0){System.out.println ("You played Rock! You have tied");
    JOptionPane.showMessageDialog(null, "Tie!");}

    else if (user == 1 && computer == 1){System.out.println ("You have played Rock! You have lost");
    JOptionPane.showMessageDialog(null, "Paper beats rock. You lose!");}

    else if (user == 1 && computer == 2){System.out.println ("You have played Rock! You have won");
    JOptionPane.showMessageDialog(null, "Rock beats scissors. You win!");}

    else if (user == 2 && computer == 0){System.out.println ("You have played Paper! You have won");
    JOptionPane.showMessageDialog(null, "Paper beats rock. You win!");}

    else if (user == 2 && computer == 1){System.out.println ("You have played Paper! You have tied");
    JOptionPane.showMessageDialog(null, "Tie!");}

    else if (user == 2 && computer == 2){System.out.println ("You have played Paper! You have lost");
    JOptionPane.showMessageDialog(null, "Scissors beats paper. You lose!");}

    else if (user == 3 && computer == 0){System.out.println ("You have played Scissors! You have lost");
    JOptionPane.showMessageDialog(null, "Rock beats Scissors. You lose!");}

    else if (user == 3 && computer == 1){System.out.println ("You have played Scissors! You have won");
    JOptionPane.showMessageDialog(null, "Scissors beats paper. You win!");}

    else if (user == 3 && computer == 2){System.out.println ("You have played Scissors! You have tied");
    JOptionPane.showMessageDialog(null, "Tie!");}



     int n = JOptionPane.showConfirmDialog(null,"Would you like to play again?", "Confirmation",JOptionPane.YES_NO_OPTION);
       if(n == JOptionPane.YES_OPTION) {
          JOptionPane.showMessageDialog(null,"Let's start");
       } else {
          JOptionPane.showMessageDialog(null,"Goodbye");
       } 

  }
 }
}

return; JOptionPane.showMessageDialog(null,"Goodbye");之后的語句JOptionPane.showMessageDialog(null,"Goodbye"); 否則,它不知道結束while循環。

如果立即選擇取消,則按取消時也會發生錯誤。 這是因為parseInt不知道如何讀取結果輸入。 您應該檢查showInputDialog的返回值以查看其有效輸出。 我建議您檢查“ 1”,“ 2”或“ 3”(或“岩石”,“紙張”或“ scizzors”),而不要使用parseInt,然后再檢查是否與之不匹配出現某種錯誤消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM