美文网首页
键盘录入x的值,计算出y的值并输出

键盘录入x的值,计算出y的值并输出

作者: Solarechos | 来源:发表于2018-07-29 20:01 被阅读0次

    /*

    键盘录入x的值,计算出y的并输出。

    x和y的关系满足如下:

    x>=3 y = 2x + 1;

    -1<=x<1 y = 2x;

    x<-1 y = 2x – 1;

    */

    import java.util.Scanner;

    class IfDemo4{

    public static void main(String[] args){

    Scanner sc = new Scanner(System.in);

    System.out.println("请输入x的值:");

    int x = sc.nextInt();

    int y;

    if(x>=3){

    y = 2*x+1;

    }else if(-1<=x && x<1){

    y = 2*x;

    }else if(x<-1){

    y = 2*x-1;

    }else{

    System.out.println("x输入的值无效,y自动赋值为0");

    y = 0;

    }

    System.out.println("y="+y);//错误: 可能尚未初始化变量y

    }

    }

    相关文章

      网友评论

          本文标题:键盘录入x的值,计算出y的值并输出

          本文链接:https://www.haomeiwen.com/subject/lijtvftx.html