f(x)=-1;x<0
0x<0
2x;x<0
老师给出,称之级联
if( x < 0 )
{
f = -1;
}
else
if( x == 0 )
{
f = 0;
}
else
{
f = 2 * x;
}
自己补充
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int x;
System.out.println("请输入数");
x = in.nextInt();
int f;
if( x < 0 )
{
f = -1;
}
else
if( x == 0 )
{
f = 0;
}
else
{
f = 2 * x;
}
System.out.println(f);
}
}
1.else要对齐
else
if( x == 0 )
{
f = 0;
}
else
{
f = 2 * x;
}
另一种做法
if ( x < 0)
{
System.out.println(-1);
}
else
{
if ( x == 0 )
{
System.out.println(0);
}
else
{
System.out.println(2 * x);
}
}
2.第一种做法更好,称为“单一出口”。
只有一个输出f,而不是各种输出。