繁体   English   中英

学习JOptionPane有麻烦吗?

[英]Having Trouble With Learning JOptionPane?

我想使用JOptionPane而不是所有内容的System.out.println。 我不知道如何描述我的程序如何跳过代码。 如果可以的话,请看一下并帮助进行下去,将不胜感激。

//Step 1: Import Java APIs
 import java.util.InputMismatchException;
 import java.util.Scanner;
 import javax.swing.*;



//Step 2: Name File and Class
 public class GolfScores {




//Step 3: Declare All Variables  

 public static void main(String[] args) {
  int hole9 = 0;
  int hole18 = 0;
  String holeChoice;

  Scanner input = new Scanner(System.in);


//Step 4: The program accepts INPUT from the user

  for (;;) {
   try {
 holeChoice =   JOptionPane.showInputDialog("For hole 9, please enter 9. If you are on hole 18, please enter 18 ");

     if (holeChoice.equals(9)) {
        System.out.println("The par for this hole is 3. Please enter if you this hole took you: "
                    + "1, 2, 3, 4, 5, 6+ shots");            
        hole9 = input.nextInt();

    } else if (holeChoice.equals(18)) {
        System.out.println("The par for this hole is 5. Please enter if you this hole took you: "
                    + "1, 2, 3, 4, 5, 6, 7 or 8+ shots");
        hole18 = input.nextInt();

    } else if (holeChoice.equals(18)) {
      System.out.println("Please enter a valid number");
      continue;
    }
    break;
} catch (InputMismatchException inputMismatchException) {
    System.err.printf("\nException: %s\n", inputMismatchException);
    System.out.println("Please enter a valid number.");
    input.next();
}
}


//Step 5 & 6: The user input is PROCESSED by Java and uses math to  calcualte an answer to output. The user's score is then output for them to see.
 if (hole18 == 1) {
   System.out.println("Your score for this hole was a hole in one!");

 } else if (hole18 == 2) {
   System.out.println("Your score for this hole was albetross.");

 } else if (hole18 == 3) {
   System.out.println("Your score for this hole was eagle.");

 } else if (hole18 == 4) {
   System.out.println("Your score for this hole was birdie.");

 } else if (hole18 == 5) {
   System.out.println("Your score for this hole was par.");

 } else if (hole18 == 6) {
   System.out.println("Your score for this hole was boogie.");

 } else if (hole18 == 7) {
   System.out.println("Your score for this hole was double boogie.");

 } else if (hole18 >= 8) {
   System.out.println("You need some more practice at golf.");

 } else if (hole18 <= 0) {
   System.out.println("Please input a valid number.");
   input.next();

 }


   if (hole9 == 1) {
   System.out.println("Your score for this hole was a hole in one!");

 } else if (hole9 == 2) {
   System.out.println("Your score for this hole was birdie.");

 } else if (hole9 == 3) {
   System.out.println("Your score for this hole was par.");

 } else if (hole9 == 4) {
   System.out.println("Your score for this hole was boogie.");

 } else if (hole9 == 5) {
   System.out.println("Your score for this hole was double boogie.");

 }  else if (hole9 >= 6) {
   System.out.println("Your need some more practice at golf.");

  }

 try {
  Thread.sleep(3000);                
}     catch(InterruptedException ex) {
      Thread.currentThread().interrupt();
}

    System.out.println("Thank you for playing Java Golf. Please come      again");

   }

}

JOptionPane.showInputDialog(..)返回的值为String,请使用:

 if (holeChoice.equals("9")) { ... }

或者使用以下方法将String解析为Integer:

Integer.parseInt(holeChoice);

JOptionPane.showInputDialog返回一个String ,您正在比较结果,就好像它是Integer

holeChoice =   JOptionPane.showInputDialog("For hole 9, please enter 9. If you are on hole 18, please enter 18 ");
//...
if (holeChoice.equals(9))  // this will always be false

如果用户输入9 ,则无法将其与int 9进行比较,您需要在所检查的值上添加引号。

例如:

if (holeChoice.equals("9"))

同样,您也可以尝试解析输入并将其转换为int ,但是您可能会打开无效用户输入的门(您需要检查该输入)

try {
   Integer choice = Integer.valueOf(holeChoice);
   //...
   if (choice.equals(9)) { // works now...
}
catch (NumberFormatException ex) {
   // user didn't enter a number!
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM