-3

//is the following code about quadratic formulas convertible to switch method?

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

//same thing repeated for letters b and c.

//int d is for discriminant.

    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");
}
user27691
  • 37
  • 2
  • 9

1 Answers1

1

If you really really really wanna use a switch case

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;
        }
    }
Nirav Shah
  • 355
  • 2
  • 6
  • Nirav Shah...I like this code actually...but one question: can I do this? Case(d>0)? – user27691 Nov 19 '15 at 20:41
  • 1
    0, 1, -1 are not the values ... they are the polarities. meaning 0 --> Zero 1 --> Positive value of 'd' -1 --> Negative value of 'd' – Nirav Shah Nov 19 '15 at 20:52
  • Aaa Nirav so interesting ! Thank you dear – user27691 Nov 19 '15 at 21:31
  • hey I just got a little problem in the answer while it was calculating. But I fixed it...It calculate the answer better in this format: int d = (int) Math.pow((b*b) - (4*a*c), 0.5); – user27691 Nov 19 '15 at 22:38