【问题标题】:JOptionPane.showConfirmDialog returns int 01 instead of String YNJOptionPane.showConfirmDialog 返回 int 01 而不是 String YN
【发布时间】:2021-12-06 02:18:50
【问题描述】:

这是我的程序的结果:

但我想做的是让房间类型的结果为 G、P 和 L,以及 Y 和 N 用于是或否问题,而不是 0123。

下面是我一直在做的代码,你能告诉我哪里做错了吗,我该怎么办?

    public static void  main(String[] args){
      JOptionPane.showMessageDialog(null, "Welcome to Easy Living Resort Hotel.\n" + "----------------------------------------------------\n" + 
        "    Room Type             Daily Rate \n" + "~~~~~~~~~~~~~      ~~~~~~~~~~\n" + "G - Garden View        Php 125.00\n" + 
        "P - Pool View             Php 145.00\n" + "L - Lake View            Php 180.00\n");

      
      /*String[] options = new String[] {"G", "P", "L"};
      int answer1 = JOptionPane.showOptionDialog(null, "G - Garden View\n" + "P - Pool View\n" + "L - Lake View\n", "Please select room type:",
        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
        null, options, options[0]);*/
      
      String answer2;
      int answer1;
      int answer3;
      int answer4;
      
      String[] options = new String[] {"G", "P", "L"};
      answer1 = JOptionPane.showOptionDialog(null, "G - Garden View\n" + "P - Pool View\n" + "L - Lake View\n", "Please select room type:",
        JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
        null, options, options[0]);
      
      answer2 = JOptionPane.showInputDialog(null, "Number of days staying: ");
      answer3 = JOptionPane.showConfirmDialog(null, "Do you want use a Refrigerator for Php 2.50 each day.");
      answer4 = JOptionPane.showConfirmDialog(null, "Do you want to avail an extra bed for Php 15.00.");
      
      String output1 = String.format("Welcome to Easy Living Resort Hotel.\n");
      String output2 = String.format("------------------------------------------------------------\n");
      String output3 = String.format("\n    Room Type             Daily Rate \n");
      String output4 = String.format("~~~~~~~~~~~~~      ~~~~~~~~~~\n");
      String output5 = String.format("G - Garden View        Php 125.00\n");
      String output6 = String.format("P - Pool View             Php 145.00\n");
      String output7 = String.format("L - Lake View            Php 180.00\n");
      String output8 = String.format("Please select room type: " + answer1);
      String output9 = String.format("\n----------------------------------------------------------\n");
      String output10 = String.format("Number of days staying: " + answer2 + "\n");
      String output11 = String.format("Refrigerator in the room? (Y/N): " + answer3 +"\n");
      String output12 = String.format("Extra bed in the room? (Y/N): " + answer4 + "\n");
      String output13 = String.format("-----------------------------------------------------------\n");      
      
      
      JOptionPane.showMessageDialog(null, output1 + output2 + output3 + output4 + output5 + output6 + output7 + output8 + output9 + output10 + output11 + output12 + output13);

【问题讨论】:

  • 也许你应该考虑写一个合适的Swing application而不是一系列JOptionPanes。

标签: java swing joptionpane


【解决方案1】:
  String[] outpyn = new String[] {"x","Y","N","N/A"};
  String output11 = String.format("Refrigerator in the room? (Y/N): " + outpyn[answer3+1] +"\n");
  String output12 = String.format("Extra bed in the room? (Y/N): " + outpyn[answer4+1] + "\n");

这是更改后的结果:

这个 Stack Overflow 问题包含部分解决方案:
What are the values for JOptionPane showConfirmDialog options?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    相关资源
    最近更新 更多