java - 如何限制用户回答?
问题描述
我正在创建一系列问题,我需要将用户输入限制为仅“A、B、C、D”,如果用户输入其他内容,则显示警告。我正在考虑创建一个if
条件来显示问题,但我不知道该怎么做。
这是我创建问题的方式:
question= "Where Adolf Hitler was born?"
+ "\nA) USA"
+ "\nB) Austria"
+ "\nC) Germany"
+ "\nD) Spain";
answer= JOptionPane.showInputDialog(question);
if(answer=="D") {
points++;
}
解决方案
我认为人们忘记了诸如此类的JOptionPane
可配置性。
从查看如何制作对话框开始了解更多详细信息
import java.text.ParseException;
import javax.swing.JOptionPane;
public class Test {
public static void main(String[] args) throws ParseException {
String message = "Where Adolf Hitler was born?";
String[] options = new String[] {
"USA", "Austria", "Germany", "Spain"
};
int selectedOption = JOptionPane.showOptionDialog(null, message, "Pick one", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, 0);
if (selectedOption == 1) {
System.out.println("Right");
} else {
System.out.println("Wrong");
}
}
}
推荐阅读
- javascript - 为什么 Form.Select 在我的简单 react-bootstrap 应用程序中没有被识别,遵循 react-bootstrap 文档?
- android - 我让我的 Android 模拟器工作,但安装 apk 时一直出现问题
- azure - 具有特定成员用户权限的 Azure Active Directory 受限来宾用户帐户
- react-native - React Native:获取比“TypeError”更详细的获取错误
- c - clBuildProgram() 在 Mac (Catalina) 上使用 clang 总是返回 -11;clGetProgramBuildInfo() 返回空字符串
- c - SDL2 SDL_Init 函数触发内存清理器错误(依赖于未初始化的数据)
- java - Spring AMQP rabbitmq retry backoff with recover 不要使来自 DLQ 的消息出列
- sql - Is there an efficient way to update MariaDB rows from a data frame in R?
- firebase - Firestore 数据的多级查询
- python - 我正在努力用 csv 模块实现 Cython