如果是,我的BlueJ在条件语句中运行了错误的行。我的节目是:
import java.io.*;
public class version_check
{
public static void main(String args[])throws IOException
{
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your BlueJ version");
String version=read.readLine();
if(version!="3.1.1")
System.out.println("You need to upgrade your BlueJ version");
else
System.out.println("You have the latest BlueJ version");
}
}
输出屏幕:
Enter your BlueJ version
3.1.1
You need to upgrade your BlueJ version
我犯了什么错误?
答案 0 :(得分:1)
使用.equals比较字符串。
if(version!="3.1.1")
System.out.println("You need to upgrade your BlueJ version");
到
if(!(version.equals("3.1.1")))
System.out.println("You need to upgrade your BlueJ version");
答案 1 :(得分:0)
我知道String类型的唯一比较方法是: 1.> String_variable.equals(String_to_be_checked_with)和 2.> String_variable.equalsIgnoresCase(String_to_be_checked_with)
第二个忽略了已经输入的字符串值的情况......
解决您问题的最简单方法是:
if(version.equals("3.1.1"))
{
System.out.println("You have the latest BlueJ version");
}
else {
System.out.println("You need to upgrade your BlueJ version");
}
答案 2 :(得分:0)
import java.io.*;
public class version_check
{
public static void main(String args[])throws IOException
{
BufferedReader read=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your BlueJ version");
String version=read.readLine();
if(version.equals("3.1.1"))
System.out.println("You need to upgrade your BlueJ
version");
else
System.out.println("You have the latest BlueJ version");
}
}