我如何使用equalsIgnorecase()的反面? [英] How do I use the opposite of equalsIgnorecase() ?

查看:119
本文介绍了我如何使用equalsIgnorecase()的反面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要完成的是要求用户输入他们的家庭成员是在工作,学习还是只是闲逛。如果答案不是前面提到的,那么问题就会重复。



这是我试图开始工作的一小段代码。就像现在一样,它只能无限循环。



What I'm trying to accomplish is to ask the user to input whether their family member is working, studying or just loafing around. If the answer is none of the aforementioned then the question is repeated.

Here's the small bit of code i'm trying to get to work. As it is right now, it only loops infinitely.

import javax.swing.JOptionPane;

public class Test{
	public static void main (String[] args){
	    
					String sisWork = JOptionPane.showInputDialog("Is she working or studying or just loafing around?");
			
				
			while(   ( !sisWork.equalsIgnoreCase("studying")) || 
				     ( !sisWork.equalsIgnoreCase("just loafing around"))    ||
				     ( !sisWork.equalsIgnoreCase("working")) ) 
			{
				sisWork = JOptionPane.showInputDialog("Error! Please enter a proper answer! working/studying/just loafing around");
			}

		}
	}

推荐答案

让我们检查逻辑:

如果用户输入'studying'=>
Let's examine the logic:
if the user enters 'studying' =>
!sisWork.equalsIgnoreCase("studying"))

将为false,同时其他2个条件为真。 ||的逻辑运算符意味着只要有一个真实,结果就是真的。其他2个选项也是如此。因此,当用户输入正确的选项时,循环将始终运行。那是不对的。

判决结果是,使用&&相反。

话虽如此,期望用户通过在输入对话框中输入一些文本然后验证输入来选择一个选项是不好的设计,考虑使用

JRadioButton [ ^ ]而不需要验证。

will be false, at the same time, the other 2 conditions will be true. The logical operator of || means as long as there is one true, the outcome is true. Same for the other 2 options. So while loop will always runs when the user enters the correct option. That is incorrect.
The verdict is, use && instead.
Having said that, it is poor design to expect users to choose an option by inputting some text into the input dialog box and then validate the input, consider using
JRadioButton[^] instead and there is no need for validation.


这篇关于我如何使用equalsIgnorecase()的反面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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