下面的代码1编译后不报错,代码2编译后就说变量没有初始化。这是为什么呢?
代码1:
import java.io.*;
public class Ex2_3_1
{
public static void main(String[] args) throws IOException
{
String str;
double y;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入x的值:");
str=input.readLine();
double x=Double.parseDouble(str);
if (x<0)
y=0;
else
y=1;
System.out.println("y= "+y);
}
}代码2:
import java.io.*;
public class Ex2_3_1
{
public static void main(String[] args) throws IOException
{
String str;
double y;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入x的值:");
str=input.readLine();
double x=Double.parseDouble(str);
if (x<0)
y=0;
else if (x<10)
y=1;
else if (x>=10)
y=2;
System.out.println("y= "+y);
}
}
代码1:
import java.io.*;
public class Ex2_3_1
{
public static void main(String[] args) throws IOException
{
String str;
double y;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入x的值:");
str=input.readLine();
double x=Double.parseDouble(str);
if (x<0)
y=0;
else
y=1;
System.out.println("y= "+y);
}
}代码2:
import java.io.*;
public class Ex2_3_1
{
public static void main(String[] args) throws IOException
{
String str;
double y;
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("请输入x的值:");
str=input.readLine();
double x=Double.parseDouble(str);
if (x<0)
y=0;
else if (x<10)
y=1;
else if (x>=10)
y=2;
System.out.println("y= "+y);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货