我正在收到用户输入并检查句子中是否有“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();
}
}
答案 0 :(得分:4)
问题在于你如何在while中创建boolean子句。所以,目前你有这个:
while(!sentence.contains(java) || !sentence.contains(Java) || !sentence.contains(JAVA))
假设句子包含" java"。所以,!sentence.contains(java)
是假的。但是,!sentence.contains(Java)
是正确的,因为句子包含" java"但不是" Java"。因为您正在使用逻辑OR(||
),所以单个true足以使整个子句成立,并且执行while循环的内部。
你可能会这样做:
while(!sentence.contains(java) && !sentence.contains(Java) && !sentence.contains(JAVA))
在这种情况下,上面的所有条款都必须为真,这意味着句子不能包含" java"," Java"或" JAVA"。
答案 1 :(得分:1)
尽量避免使用" Java"的所有不同变量。我想你是在试图找到java出现的时候。所以适用于你的句子一个句子.toLowerCase()。contains(" java") 用户输入将转换为小写,只检查是否包含小写的单词java,这样就可以避免使用这么多或者。 在你的内部只需要一段时间(真实),并检查if(sentence.toLowerCase()。contains(" java")){break;}。