Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
While循环While字符串不等于多个其他字符串(Java)_Java_While Loop_String Comparison - Fatal编程技术网

While循环While字符串不等于多个其他字符串(Java)

While循环While字符串不等于多个其他字符串(Java),java,while-loop,string-comparison,Java,While Loop,String Comparison,我正在编写一个方法,并希望以比较开始该方法,以查看存储的静态字符串是否等于其他单词。此处为psuedocode格式: While (String1 is neither equal to "A" nor "B" nor "C" nor "D") { Print (Please enter a correct option); Take input; } 任何帮助都非常感谢。正如Hemant在中所述,使用集合存储您要检查的字符串是明智的: Set<String> wo

我正在编写一个方法,并希望以比较开始该方法,以查看存储的静态字符串是否等于其他单词。此处为psuedocode格式:

While (String1 is neither equal to "A" nor "B" nor "C" nor "D")
{
    Print (Please enter a correct option);
    Take input;
}
任何帮助都非常感谢。

正如Hemant在中所述,使用
集合
存储您要检查的
字符串是明智的:

Set<String> words = Set.of("Hello", "World!");

while (!words.contains(string1)) {
    ...
}
Set words=Set.of(“你好”,“世界!”);
而(!words.contains(string1)){
...
}
注意
Set#of
是在Java 9中引入的,而
string1
是您要检查的
字符串。

//静态设置在类的顶部
// static set at top of the class
private static final Set<String> SET = new HashSet<>
                                (Arrays.asList("A", "B", "C", "D"));

void func() {

    String input = takeInput();
    while(!SET.contains(input)) {

        input = takeInput();
    }
}
私有静态最终集=新哈希集 (A、B、C、D等); void func(){ 字符串输入=takeInput(); 而(!SET.contains(输入)){ input=takeInput(); } }
一种方法是构造一个简单的匹配字符串的方法:

String input = "";
while (!Pattern.matches("FirstString|SecondString|ThirdString", input)) {
    ...
}
正则表达式模式由您希望允许的单词组成,这些单词由竖条
|
字符分隔

注意:使用
循环需要在进入循环之前将
输入设置为不匹配的
字符串。您可以通过在循环时更改为
do
/
来改进此功能:

String input;
do {
    System.out.println("Please enter a correct option");
    input = takeInput();
} while (!Pattern.matches("FirstString|SecondString|ThirdString", input));

在本例中,您有四个可接受的输入,即
“A”、“B”、“C”、“D”
。这可以很容易地实现只使用布尔运算符;也就是说,循环条件是当您的输入既不是
“A”
的输入,也不是
“B”
的输入,也不是
“C”
的输入,也不是
“D”

但是,如果存在任意但有限多的可接受输入,比如匈牙利字母表中的所有字母,情况又如何呢?这套可能是最好的选择

Scanner input = new Scanner(System.in);

// Either manually or use a method to add admissible values to the set
Set<String> admissible = new HashSet<String>();
admissible.add("A");
admissible.add("B");
admissible.add("C");
admissible.add("D");
// ... and so on. You can also use helper methods to generate the list of admissibles
// in case if it is in a file (such as a dictionary or list of words).

System.out.println("Enter a selection (Set): ");
String string1 = input.next();

// If input string1 is not found anywhere in the set
while (!admissible.contains(string1))  {
    System.out.println("Enter again (Set): ");
    string1 = input.next();
} 
input.close();
扫描仪输入=新扫描仪(System.in);
//手动或使用方法向集合中添加允许的值
Set acceptable=新的HashSet();
可接受。添加(“A”);
可接受。添加(“B”);
可接受。添加(“C”);
可接受。添加(“D”);
// ... 等等您还可以使用助手方法生成可接受项列表
//如果它在文件中(如字典或单词列表)。
System.out.println(“输入选择(集合):”;
字符串string1=input.next();
//如果在集合中的任何位置都找不到输入字符串1
而(!容许.contains(string1)){
System.out.println(“再次输入(设置):”;
string1=input.next();
} 
input.close();

创建一组a、B、C、D,并检查
集合。包含(string1)
在while循环中我更喜欢使用
do while循环
不知道为什么,但大写的
集合
让我很困扰,它可以更改为有意义的名称,因为我们不知道上下文,我们说不出他确切的名字variable@NiVeR常量可以大写。困扰我的是一个毫无意义的名字
SET
@lexicore,我确实是指这个。常量的大写字母是非常好的。另外,哪种方式最快:使用布尔值、使用集合还是使用正则表达式?只有使用好的基准测试,您才能明确地回答性能问题。我打赌那是
!字符串1.等于(“A”)&!string1.equals(“B”)&&……
对于少量元素来说是最有效的。在一定的数字下,这一套会变得更好。我不认为正则表达式是最差的变体,因为它本质上与
相同!字符串1.等于(“A”)&!string1.equals(“B”)&&……
,但有一些开销。当然。是否建议我注意第一种方法,仅使用布尔值的“naive”或“plain”方法,对于少量元素最为推荐?还是评论足够好?那么,这是“最受推荐的”吗?你有证据吗?我的评论只是我的猜测。那么,即使假设这是最有效的方式,也不建议这样做。我个人更喜欢使用具有适当实现的set。我是否需要为模式函数导入一些内容?@lexicore这取决于基准测试。在需要用户输入的情况下,即使使用100多个项目的线性阵列搜索,也不会有明显的差异,即使是由经过训练的马戏团老鼠执行;-)
Scanner input = new Scanner(System.in);

// Either manually or use a method to add admissible values to the set
Set<String> admissible = new HashSet<String>();
admissible.add("A");
admissible.add("B");
admissible.add("C");
admissible.add("D");
// ... and so on. You can also use helper methods to generate the list of admissibles
// in case if it is in a file (such as a dictionary or list of words).

System.out.println("Enter a selection (Set): ");
String string1 = input.next();

// If input string1 is not found anywhere in the set
while (!admissible.contains(string1))  {
    System.out.println("Enter again (Set): ");
    string1 = input.next();
} 
input.close();