为什么我必须在我的循环语句中声明另一个字符串比较?
Why do I have to declare another string comparison in my loop statement?
所以我们的教授让我们做一个循环,如果我们想继续,我们会问一个问题,如果用户输入 "Y" 它会再次询问等等。但是一旦用户输入 "N",它会说 "Good Bye!".. 我尝试使用 if 语句,当用户输入 "N" 时,它会再次询问,它没有以 "Good Bye!" 结束。所以在偶然发现这里并设法让它工作之后..我只想问,为什么我必须将 input.equals("Y");
放在循环语句中?
提前致谢!
import java.io.*;
import java.util.*;
public class While{
public static void main(String Args[]){
String input;
String b="Y";
String c="N";
Scanner get=new Scanner(System.in);
System.out.println("Please enter 'Y' for YES and 'N' for NO.");
System.out.print("Do you want to continue? ");
input = get.nextLine().toUpperCase();
while (input.equals(b))
{
input.equals("Y");
System.out.print("Do you want to continue? ");
input = get.nextLine().toUpperCase();
}
System.out.println("Good Bye!");
}
}
input.equals("Y")
只是检查 input
是否等于 "Y"
和 returns true
如果是,否则 false
。但是由于你根本没有使用 .equals
的 return 值,所以这一行完全没有用。
所以我们的教授让我们做一个循环,如果我们想继续,我们会问一个问题,如果用户输入 "Y" 它会再次询问等等。但是一旦用户输入 "N",它会说 "Good Bye!".. 我尝试使用 if 语句,当用户输入 "N" 时,它会再次询问,它没有以 "Good Bye!" 结束。所以在偶然发现这里并设法让它工作之后..我只想问,为什么我必须将 input.equals("Y");
放在循环语句中?
提前致谢!
import java.io.*;
import java.util.*;
public class While{
public static void main(String Args[]){
String input;
String b="Y";
String c="N";
Scanner get=new Scanner(System.in);
System.out.println("Please enter 'Y' for YES and 'N' for NO.");
System.out.print("Do you want to continue? ");
input = get.nextLine().toUpperCase();
while (input.equals(b))
{
input.equals("Y");
System.out.print("Do you want to continue? ");
input = get.nextLine().toUpperCase();
}
System.out.println("Good Bye!");
}
}
input.equals("Y")
只是检查 input
是否等于 "Y"
和 returns true
如果是,否则 false
。但是由于你根本没有使用 .equals
的 return 值,所以这一行完全没有用。