Java input.equals(“此处为字符串”)不起作用
我是Java新手。我试图制作一个程序,用户输入一个国家,然后返回该国家的当前时间。我有以下代码:Java input.equals(“此处为字符串”)不起作用,java,string,timezone,Java,String,Timezone,我是Java新手。我试图制作一个程序,用户输入一个国家,然后返回该国家的当前时间。我有以下代码: public static void main(String[] args) { Scanner userInput = new Scanner(System.in); System.out.println("Enter a country: "); String userCountryInput = userInput.nextLine(); if (userI
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter a country: ");
String userCountryInput = userInput.nextLine();
if (userInput.equals("Philippines")) {
Date date1 = new Date();
System.out.println(date1);
}
if (userInput.equals("Russia")) {
TimeZone.setDefault(TimeZone.getTimeZone("UTC + 05:30"));
Date date2 = new Date();
System.out.println(date2);
}
}
当我输入“俄罗斯”或“菲律宾”时,它不会输出任何内容。知道为什么吗?
userCountryInput
是输入变量userInput
是用于获取输入的扫描仪变量
if ("Philippines".equals(userCountryInput)) {
userCountryInput
是输入变量userInput
是用于获取输入的扫描仪变量
if ("Philippines".equals(userCountryInput)) {
userCountryInput
是输入变量userInput
是用于获取输入的扫描仪变量
if ("Philippines".equals(userCountryInput)) {
userCountryInput
是输入变量userInput
是用于获取输入的扫描仪变量
if ("Philippines".equals(userCountryInput)) {
您在
userInput
上使用的是equals,它是扫描器而不是字符串。它将用于字符串userCountryInput
:
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter a country: ");
String userCountryInput = userInput.nextLine();
if (userCountryInput .equals("Philippines")){
Date date1 = new Date();
System.out.println(date1);
}
if (userCountryInput .equals("Russia")){
TimeZone.setDefault(TimeZone.getTimeZone("UTC + 05:30"));
Date date2 = new Date();
System.out.println(date2);
}
}
您在
userInput
上使用的是equals,它是扫描器而不是字符串。它将用于字符串userCountryInput
:
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter a country: ");
String userCountryInput = userInput.nextLine();
if (userCountryInput .equals("Philippines")){
Date date1 = new Date();
System.out.println(date1);
}
if (userCountryInput .equals("Russia")){
TimeZone.setDefault(TimeZone.getTimeZone("UTC + 05:30"));
Date date2 = new Date();
System.out.println(date2);
}
}
您在
userInput
上使用的是equals,它是扫描器而不是字符串。它将用于字符串userCountryInput
:
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter a country: ");
String userCountryInput = userInput.nextLine();
if (userCountryInput .equals("Philippines")){
Date date1 = new Date();
System.out.println(date1);
}
if (userCountryInput .equals("Russia")){
TimeZone.setDefault(TimeZone.getTimeZone("UTC + 05:30"));
Date date2 = new Date();
System.out.println(date2);
}
}
您在
userInput
上使用的是equals,它是扫描器而不是字符串。它将用于字符串userCountryInput
:
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter a country: ");
String userCountryInput = userInput.nextLine();
if (userCountryInput .equals("Philippines")){
Date date1 = new Date();
System.out.println(date1);
}
if (userCountryInput .equals("Russia")){
TimeZone.setDefault(TimeZone.getTimeZone("UTC + 05:30"));
Date date2 = new Date();
System.out.println(date2);
}
}
正如其他人所说,您将
userInput
与userCountryInput
混为一谈
您应该为变量使用更好的名称,这样以后就不会再将它们混淆了。例如,可能值得将
userCountryInput
重命名为userCountry
正如其他人所说,您将userInput
与userCountryInput
混为一谈
您应该为变量使用更好的名称,这样以后就不会再将它们混淆了。例如,可能值得将
userCountryInput
重命名为userCountry
正如其他人所说,您将userInput
与userCountryInput
混为一谈
您应该为变量使用更好的名称,这样以后就不会再将它们混淆了。例如,可能值得将
userCountryInput
重命名为userCountry
正如其他人所说,您将userInput
与userCountryInput
混为一谈
您应该为变量使用更好的名称,这样以后就不会再将它们混淆了。例如,如果(userInput==“菲律宾”)给我一个错误,那么将
userCountryInput
重命名为userCountry
可能是值得的。它说“不可比较的类型:扫描器和字符串”,这毫无意义-只需将从nextLine
返回的字符串与文本值进行比较即可。好的,对不起,我知道了。我就在那里说的新秀的话。非常感谢。Reimeus@DylanVillaruel既然你似乎已经知道了,为什么还要用==
来比较字符串?@Pshemo我刚刚知道,就像几分钟前一样。我对函数还不太了解。如果(userInput==“菲律宾”)给我一个错误。它说“不可比较的类型:扫描器和字符串”,这毫无意义-只需将从nextLine
返回的字符串与文本值进行比较即可。好的,对不起,我知道了。我就在那里说的新秀的话。非常感谢。Reimeus@DylanVillaruel既然你似乎已经知道了,为什么还要用==
来比较字符串?@Pshemo我刚刚知道,就像几分钟前一样。我对函数还不太了解。如果(userInput==“菲律宾”)给我一个错误。它说“不可比较的类型:扫描器和字符串”,这毫无意义-只需将从nextLine
返回的字符串与文本值进行比较即可。好的,对不起,我知道了。我就在那里说的新秀的话。非常感谢。Reimeus@DylanVillaruel既然你似乎已经知道了,为什么还要用==
来比较字符串?@Pshemo我刚刚知道,就像几分钟前一样。我对函数还不太了解。如果(userInput==“菲律宾”)给我一个错误。它说“不可比较的类型:扫描器和字符串”,这毫无意义-只需将从nextLine
返回的字符串与文本值进行比较即可。好的,对不起,我知道了。我就在那里说的新秀的话。非常感谢。Reimeus@DylanVillaruel既然你似乎已经知道了,为什么还要用==
来比较字符串?@Pshemo我刚刚知道,就像几分钟前一样。我对函数还不太了解。谢谢你!我现在明白了谢谢你!我现在明白了谢谢你!我现在明白了谢谢你!我现在明白了,要书会引起讨论,这不是本网站的目的。在我看来,如果Java是第一语言,你正在学习,你应该选择一本母语书。大多数的书都会涵盖基本的需求,在你开始学习之后,你可以通过论坛、API或解决问题来获得你所需要的知识。索取书籍会引发讨论,而这不是本网站的目的。在我看来,如果Java是第一语言,你正在学习,你应该选择一本母语书。大多数的书都会涵盖基本的需求,在你开始学习之后,你可以通过论坛、API或解决问题来获得你所需要的知识。索取书籍会引发讨论,而这不是本网站的目的。在我看来,如果Java是第一语言,你正在学习,你应该选择一本母语书。大多数的书都会涵盖基本的需求,在你开始学习之后,你可以通过论坛、API或解决问题来获得你所需要的知识。索取书籍会引发讨论,而这不是本网站的目的。在我看来,如果Java是第一语言,你正在学习,你应该选择一本母语书。大多数的书都会涵盖基本的需求,在你开始学习之后,你会发现你的书很有趣