gpt4 book ai didi

java - while 循环 : while sentence does not contain a word

转载 作者:行者123 更新时间:2023-11-29 07:28:43 24 4
gpt4 key购买 nike

我正在获取用户输入并检查句子中是否包含“java”一词。我做了一个 while 循环,但即使句子中有“java”这个词,它也告诉我它不是,并继续 while 循环。如果我删除 while 循环,我希望我的程序执行的所有其他操作都有效。这是代码:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

//declare all variables
String sentence;
int java_index_number;
String stop_program;
String java;
String Java;
String JAVA;
String java_capital_first_letter;
String java_all_caps;

//scanner to get user input
Scanner user_input = new Scanner(System.in);

//prompt the user for a sentence
System.out.print("Enter a line of text containing the word 'java' somewhere within it: ");
sentence = user_input.nextLine();

java = "java";
Java = "Java";
JAVA = "JAVA";
while(!sentence.contains(java) || !sentence.contains(Java) || !sentence.contains(JAVA)) {
System.out.println("Your sentence does not have the word 'java' within it.");
System.out.print("Enter a line of text containing the word 'java' somewhere within it: ");
sentence = user_input.nextLine();
}

//output
System.out.println();
System.out.println("The string read is: " + sentence);
System.out.println("Length in chars is: " + sentence.length());
System.out.println("All lowercase is: " + sentence.toLowerCase());
System.out.println("All uppercase is is: " + sentence.toUpperCase());

//store java index pos in variable
java_index_number = sentence.indexOf("java");
System.out.println("Found 'java' or at pos: " + java_index_number);


//make first letter of java a capital letter
java_capital_first_letter = sentence.substring(0, java_index_number) + sentence.substring(java_index_number,
java_index_number + 1).toUpperCase() + sentence.substring(java_index_number + 1, java_index_number + 4)
+ sentence.substring(java_index_number + 4);

//make java all caps
java_all_caps = sentence.substring(0, java_index_number) + sentence.substring(java_index_number,
java_index_number + 4).toUpperCase() + sentence.substring(java_index_number + 4);

//output
System.out.println("Changing to 'Java': " + java_capital_first_letter);
System.out.println("Changing to 'JAVA': " + java_all_caps);

// Keep console window alive until 'enter' pressed
System.out.println();
System.out.println("Done - press enter key to end program");
stop_program = user_input.nextLine();


}
}

如何让 while 循环工作?

更新:在收到很棒的反馈后,我终于开始工作了。感谢所有提供帮助的人!

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

//declare all variables
String sentence;
int java_index_number;
String stop_program;
String java_capital_first_letter;
String java_all_caps;

//scanner to get user input
Scanner user_input = new Scanner(System.in);

//prompt the user for a sentence
System.out.print("Enter a line of text containing the word 'java' somewhere within it: ");
sentence = user_input.nextLine();


while(!sentence.toLowerCase().contains("java")) {
System.out.println("Your sentence does not have the word 'java' within it.");
System.out.print("Enter a line of text containing the word 'java' somewhere within it: ");
sentence = user_input.nextLine();
}

//output
System.out.println();
System.out.println("The string read is: " + sentence);
System.out.println("Length in chars is: " + sentence.length());
System.out.println("All lowercase is: " + sentence.toLowerCase());
System.out.println("All uppercase is is: " + sentence.toUpperCase());

//store java index pos in variable
sentence = sentence.toLowerCase();
java_index_number = sentence.indexOf("java");
System.out.println("Found 'java' or at pos: " + java_index_number);


//make first letter of java a capital letter
java_capital_first_letter = sentence.substring(0, java_index_number) + sentence.substring(java_index_number,
java_index_number + 1).toUpperCase() + sentence.substring(java_index_number + 1, java_index_number + 4)
+ sentence.substring(java_index_number + 4);

//make java all caps
java_all_caps = sentence.substring(0, java_index_number) + sentence.substring(java_index_number,
java_index_number + 4).toUpperCase() + sentence.substring(java_index_number + 4);

//output
System.out.println("Changing to 'Java': " + java_capital_first_letter);
System.out.println("Changing to 'JAVA': " + java_all_caps);

// Keep console window alive until 'enter' pressed
System.out.println();
System.out.println("Done - press enter key to end program");
stop_program = user_input.nextLine();


}
}

最佳答案

问题在于您如何在 while 中创建 boolean 子句。所以,目前你有这个:

while(!sentence.contains(java) || !sentence.contains(Java) || !sentence.contains(JAVA))

假设句子中包含“java”。所以,!sentence.contains(java) 是错误的。但是,!sentence.contains(Java) 是正确的,因为该句子包含“java”但不包含“Java”。因为您使用的是逻辑 OR (||),所以单个 true 足以使整个子句为 true,并执行 while 循环的内部。

您尝试做的事情可能会以这种方式完成:

while(!sentence.contains(java) && !sentence.contains(Java) && !sentence.contains(JAVA))

在这种情况下,上述所有子句都必须为真,这意味着该句子不能包含“java”、“Java”或“JAVA”。

关于java - while 循环 : while sentence does not contain a word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46208734/

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