编写的时候卡住了,不知道这么写,求大哥大帮帮忙。
if语句编程:根据输入的x值,输出y的值。
x<0 , y=2x+1;
x=0 , y=2;
0<x<=3 , y=3x-2;
x>3 , y=6x
import java.util.Scanner;
public class Test{
public static void main(String []args){
Scanner in=new Scanner(System.in);
int x=in.nextInt();
int y=0;
if (x<0)
y=2*x+1;
else if(x==0)
y=2;
else if(x>0 &&x<=3)
y=3*x-2;
else
y=6*x;
System.out.println("y="+y);
}
}
}
代码如下:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y;
if(x<0){
y = 2*x+1;
}else if(x==0){
y = 2;
}else if(x>0 && x<=3){
y = 3*x-2;
}else
y = 6*x;
System.out.println("y="+y);
}
float x,y;
if(x<0)
y = 2*x+1;
else if(x==0)
y = 2;
else if(x>3)
y = 6*x;
else
y = 3*x-2;