考试题2

有一个函数

y=x  (x<1)

y=2x-1  (1<=x<10)

y=3x-11  (x>=10)

写一个方法getValue(),对任意参数x,返回y的值

编写方法getValue

public class test{
	public static void main(String[] args) {
		
	}
	public static int getValue(int x){
        
        }

用if else 判断x的条件和y的值

public class test{
	public static void main(String[] args) {
		
	}
	public static int getValue(int x){
		int y=0;
		if(x<1){
			y=x;
		}else if(1<=x && x<10){
			y=2*x-1;
		}else{
			y=3*x-11;
		}

	}
}

返回y 并输出在主方法里传参数

public class test{
	public static void main(String[] args) {
		int y=getValue(0);
	}
	public static int getValue(int x){
		int y=0;
		if(x<1){
			y=x;
		}else if(1<=x && x<10){
			y=2*x-1;
		}else{
			y=3*x-11;
		}
		return y;

	}
}

运行结果:

原文地址:https://www.cnblogs.com/zyn0216/p/7501952.html