使用if语句重新提示用户输入数据 [英] Re-prompting the user for data input with an if statement

查看:401
本文介绍了使用if语句重新提示用户输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在此代码中提示输入名字/姓氏,但如果他们忘记填写该字段,我无法弄清楚如何重新提示用户输入该名称。现在,如果我的代码没有得到任何输入并且用户点击向前,我就会弹出一个消息框,然后程序将关闭。我希望能够在没有完全失序的情况下重新提示完全相同的步骤。

I'm trying to prompt for a first/last name in this code, but I cannot figure out how to re-prompt a user for the name if they forget to fill out the field. Right now if my code does not get any input and the user clicks onwards, I have it so a message box will pop up and then the program will close. I want to be able to just re-prompt for the exact same step without it getting all out of order.

这是我到目前为止所做的:

This is what I have so far:

public void getValues(){
    firstName = JOptionPane.showInputDialog(null, "Please enter employee's first name");

    if (firstName.equals("")){

        JOptionPane.showMessageDialog(null,"Please enter a first name, next time.");
        System.exit(-1);
    } else {

    lastName = JOptionPane.showInputDialog(null, "Please enter employee's last name");

    if (lastName.equals("")){

        JOptionPane.showMessageDialog(null,"Please enter a last name, next time.");
        System.exit(-1);
    } else {

谢谢。

推荐答案

在必要时使用循环,直到获得所需的输入。

Use a while loop where necessary until you get the input you want.

这是一个片段。

String firstname = JOptionPane.showInputDialog(null, "Name?");
while(firstname.equals("")) {
    firstname = JOptionPane.showInputDialog(null, "You need to enter a first name!);
}

这篇关于使用if语句重新提示用户输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆