如何更改此if语句以进行切换?

时间:2015-11-19 20:00:10

标签: java if-statement switch-statement quadratic determinants

//是关于可转换为switch方法的二次公式的以下代码?

Public class blah blah
  Public static void main(String[] args) {
    System.out.println("enter letter a");
    New Scanner= System.in
    Int a = input.nextint()

//对于字母b和c重复同样的事情。

// int d用于判别。

    Int d= math.pow(b^2 -4ac, 0.5);
    Int r1= (-(b) - (d))/2(a)
    Int r2= (-(b) + (d))/2(a)

    If(d>0){
      System.out.println("2 solutions: r1; " + r1+ " and r2" + r2);
    }else if(d=0){
      System.out.println("1 solutions: r1; " + r1+ " and r2" + r2);
    }else{
      System.out.println("no real solution");
}

1 个答案:

答案 0 :(得分:1)

如果你真的真的想使用开关盒

private static void switchOnIntegerPolarity() {
        int a = 1;
        int b = 2;
        int c = 3;
        int d = (int) Math.pow(b ^ 2 - 4 * a * c, 0.5);
        int r1 = (-(b) - (d)) / 2 * (a);
        int r2 = (-(b) + (d)) / 2 * (a);

        switch ((int) Math.signum(d)) {
        case 0: // Zero
            System.out.println("1 solutions: r1; " + r1 + " and r2" + r2);
            break;
        case 1: // 'd' is Positive
            System.out.println("2 solutions: r1; " + r1 + " and r2" + r2);
            break;
        case -1: // 'd' is Negative
            System.out.println("no real solution");
            break;
        }
    }