gpt4 book ai didi

java - JOptionPane 的小问题

转载 作者:行者123 更新时间:2023-12-02 04:17:21 25 4
gpt4 key购买 nike

我尝试使用 JOptionPane 运行我的程序,但它没有输出错误窗口。我的程序必须从文件中读取乘客,他们都有名字、姓氏、航类舱位、护照代码和通关统计信息。如果乘客有黄色、橙色或红色的许可警告,则应输出一个 JOptionPane 对话框。消息框的标题应为“安全许可警报”。

-如果 ClearanceException 为黄色,则显示“小心继续”。- 如果 ClearanceException 为橙色,则显示“已识别出可能的威胁。”。- 如果 ClearanceException 为红色,则显示“已识别威胁。请联系执法部门”。

我的问题是,每个乘客都会出现一个黄色警告对话框,而橙色和红色警告永远不会出现。这是我的代码

驱动程序/主控

package lab.assignment.pkg11;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.SecurityException;
import java.nio.file.Paths;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.JOptionPane;

public class ClearanceException
{
private static Scanner input;

public static void main(String[] args)
{
String fName;
String lName;
String fClass;
String pCode;
String securityStatus;

code clCode = code.Yellow;

try
{
input = new Scanner(Paths.get("Passengers.dat"));
input.useDelimiter("[\r\n,]");

while (input.hasNext())
{
fName = input.next();
lName = input.next();
fClass = input.next();
pCode = input.next();

System.out.printf("%s, %s, %s,%s, %s%n", fName, lName, fClass, pCode, clCode);

switch (clCode)
{
case Yellow:
JOptionPane.showMessageDialog(null, "Proceed with caution.", "Code Yellow", JOptionPane.ERROR_MESSAGE);
break;

case Orange:
JOptionPane.showMessageDialog(null, "Possible threat identified.", "Code Orange", JOptionPane.ERROR_MESSAGE);
break;

case Red:
JOptionPane.showMessageDialog(null, "Threat identified. Contact law enforcement.", "Code Red", JOptionPane.ERROR_MESSAGE);
break;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
enum code {Green, Yellow, Orange, Red;}
}

类别

`package lab.assignment.pkg11;

public class ClearanceMain extends Exception
{
public ClearanceMain (String message)
{
super (message);
}

ClearanceMain()
{
throw new UnsupportedOperationException("Not supported yet.");
}
}`

记事本文件

`Bryan,Buonaiuto,econo,USA,Green
Emily,Cativo,busin,USA,Yellow
Edmond,Wint,first,USA,Orange
Eric,Monforte,busin,ITA,Red
James,Kilmeade,econo,USA,Green
Alexander,Antonacci,econo,PRT,Green
Gabriella,Johnson,first,CAN,Green
Barbara,Martinez,first,COL,Orange
Enam,Safi,econo,YEM,Orange
Sean,Yakub,busin,YEM,Orange
Christina,Tarin,busin,CMR,Green
Emily,Sharma,econo,CMR,Green
Charnelle,Kupfer,econo,DEU,Green
Aaron,Gossett,econo,DEU,Green
Conrad,Golder,econo,USA,Green
Carla,Vasquez,first,USA,Green
Melinda,Osorio,first,DOM,Green
Antonio,Espinoza,econo,DOM,Green
Seth,Howell,busin,USA,Orange
Navpreet,Afzal,busin,PAK,Red
Thomas,Przywara,busin,BEL,Green
Lea,Gaang,econo,BEL,Green
Phoebe,Starks,first,USA,Green
Netel,Abdelghani-Serour,econo,PAK,Green
Ayush,Juzumas,econo,AGO,Green
Ayesha,Saagber,first,AGO,Green
Darla,Zagorski,busin,DEU,Green
Ling,Weng,first,CHIN,Yellow
Chin,Weng,first,CHIN,Yellow
Adbdul,Islam,econo,PAK,Red
Gissele,Bencosme,econo,USA,Green
Ismanel,Kefalas,busin,IND,Green
Lee,Kang,busin,KOR,Green
Graciela,Quinones,econo,SLV,Green
Jorges,Quinones,econo,SLV,Green
Lissette,Quinones,econo,SLV,Green
Nutan,Patel,first,IND,Green
Wilson,Singh,first,IND,Green`

最佳答案

你忘了:

clCode = input.next();

在此之后:

fName = input.next();
lName = input.next();
fClass = input.next();
pCode = input.next();

您无法从文本文件中获取颜色,因此在此处设置的 clCode 始终为 Yellow:

code clCode = code.Yellow;

当然,input.next() 返回一个String,因此您必须解析它以找出它对应于枚举中的哪个值。您可以使用方法 Enum#valueOf() 来执行此操作:

code clCode = code.valueOf(input.next());

只要字符串与枚举值完全匹配,该方法就可以工作。

关于java - JOptionPane 的小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com