Java语言程序设计 题目:编写一个程序,求Y值。要求用if-else 编写 往下面看看

java语言程序设计编程

import java.util.Scanner;

public class C {

public static void main(String[] args) {
int x;
int y=0;
System.out.println("请输入X的值");
Scanner q = new Scanner(System.in);
x = q.nextInt();
if (x > 0)
{
y= x + 2;
}
else if (x == 0)
{
y= x - 2;
}
else if (x < 0)
{
y= x - 4;
}
System.out.println("y="+y);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-29
public static void main(String[] args) {
try{
Scanner sc = new Scanner(System.in);
System.out.print("请输入X的值: ");
int x = sc.nextInt();
int value = getY(x);
System.out.println("y = " + value);
}catch(Exception e){
System.out.println("请输入数字");
}

}
private static int getY(int x) {
int value = 0;
if(x > 0){
value = x + 2;
}else if(x < 0){
value = x - 4;
}else{
value = x - 2;
}
return value;
}
第2个回答  2013-09-29
Scanner sc=new Scanner(System.in);
System.out.println("请输入x的值");
int x=sc.nextInt();
int y=0;
if(x>0){
y=x+2;
}else if(x==0){
y=x-2;
}else{
y=x-4;
}
System.out.println(y);
第3个回答  2013-09-29
import java.util.Scanner;

public class Test {

public static void main(String[] args) {
Test t = new Test();
t.test();
}

public void test() {
System.out.println("请出入x的值:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
if (x > 0) {
x += 2;
} else if (x == 0) {
x -= 2;
} else {
x -= 4;
}
int y = x;
System.out.println(y);
}

}
第4个回答  2013-09-29
if(x>0) Y=x+2;
else if(x<0) Y=x-4;
else Y=x-2;  // x=0
System.our.println("Y="+Y);

第5个回答  2013-09-29
public int getY(int x)
{
if (x > 0)
{
return x + 2;
}
else if (x == 0)
{
return x - 2;
}
else if (x < 0)
{
return x - 4;
}
return 0;

}