JOptionPane组件返回值显示问题求助:需将数字结果替换为指定字符(G/P/L/Y/N)
问题分析与修正方案
嘿,我看了你的代码,问题很明确——你直接把JOptionPane返回的整数结果输出了,没有把它们转换成你想要的字符(G/P/L和Y/N)。咱们一步步来解决:
错误点梳理
直接输出整数而非目标字符
你在output8、output11、output12里直接拼接了answer1、answer3、answer4的整数值,但这些是JOptionPane的返回码:- 房间类型选择的
0、1、2对应你定义的选项"G"、"P"、"L" showConfirmDialog返回的0代表YES,1代表NO,你需要把它们转成Y和N
- 房间类型选择的
String.format用法不规范
你调用了String.format但还是用+直接拼接字符串,这完全浪费了format方法的格式化优势,应该用占位符(%s、%d等)来组织内容,代码更清晰也更易维护。
修正方案与完整代码
下面是修改后的代码,我标注了关键修改点:
import javax.swing.JOptionPane; public class HotelReservation { 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 = JOptionPane.showInputDialog(null, "Number of days staying: "); int answer3 = JOptionPane.showConfirmDialog(null, "Do you want use a Refrigerator for Php 2.50 each day."); int answer4 = JOptionPane.showConfirmDialog(null, "Do you want to avail an extra bed for Php 15.00."); // 关键修改1:将房间类型整数映射为G/P/L String roomType; switch(answer1) { case 0: roomType = "G"; break; case 1: roomType = "P"; break; case 2: roomType = "L"; break; default: roomType = "Unknown"; // 处理用户意外操作的情况 } // 关键修改2:将确认对话框结果映射为Y/N,用JOptionPane常量更易读 String fridgeOption = (answer3 == JOptionPane.YES_OPTION) ? "Y" : "N"; String bedOption = (answer4 == JOptionPane.YES_OPTION) ? "Y" : "N"; // 关键修改3:用String.format的占位符规范格式化输出 String output1 = String.format("Welcome to Easy Living Resort Hotel.%n"); String output2 = String.format("------------------------------------------------------------%n"); String output3 = String.format(" 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: %s%n", roomType); String output9 = String.format("----------------------------------------------------------%n"); String output10 = String.format("Number of days staying: %s%n", answer2); String output11 = String.format("Refrigerator in the room? (Y/N): %s%n", fridgeOption); String output12 = String.format("Extra bed in the room? (Y/N): %s%n", bedOption); String output13 = String.format("-----------------------------------------------------------%n"); JOptionPane.showMessageDialog(null, output1 + output2 + output3 + output4 + output5 + output6 + output7 + output8 + output9 + output10 + output11 + output12 + output13); } }
额外说明
- 我加入了
default分支处理房间类型的意外情况,避免用户触发未知返回码时输出混乱; - 使用
JOptionPane.YES_OPTION而不是硬写0,让代码可读性更高,即使你忘记了返回码的具体数值,也能一眼看懂逻辑; - 用
%n代替\n作为换行符,让输出在不同操作系统上都能正常显示。
内容的提问来源于stack exchange,提问作者Syl
相关产品推荐
相关产品推荐

