burakburi
burakburi

Reputation: 23

program that prints the result to linear equation of : ax+b = 0

Is it possible to write less for example by writing the conditions into one if- instruction or something like that.My point is to cut the source code and find a better way to sum the informations in the if instruction up.

import java.util.*; 
public class LinGlj4u {

    public static void main(String[] args) {        

        Scanner input = new Scanner(System.in);
        System.out.print("Bitte geben sie den Wert für a ein : ");
        double a = input.nextDouble();
        System.out.print("Bitte geben sie den Wert für b ein : ");
        double b = input.nextDouble();

        double rechnung;
        if (a + b > 0) {
            rechnung = -b / a;
            System.out.println(rechnung);
        } else if (a + b < 0) {
            rechnung = +b / a;
            System.out.println(rechnung);
        } else if (a + b == 0 || a - b == 0) {
            System.out.println("x = 0");
        }
    }
}

Upvotes: 1

Views: 4016

Answers (4)

Matthew Zackschewski
Matthew Zackschewski

Reputation: 486

Hope this is short enough for you. There's probably a better shorter way using Regex like Jossef showed in his example. The last else if condition isn't important so just making it else should do the trick as well.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    // No need to ask twice, seperate inputs by space
    System.out.print("Bitte geben sie den Wert für a ein : ");
    Double a = input.nextDouble();
    Double b = input.nextDouble();
    Double rechnung;

    if (a != 0)
        rechnung = -b/a;
    else {
        System.out.println("x goes to infinity");
        return;
    }
    System.out.println(rechnung);
}

Upvotes: 0

Jossef Harush Kadouri
Jossef Harush Kadouri

Reputation: 34227

I Would simplify it to the following

import java.util.*; 
public class LinGlj4u {

public static void main(String[] args) {        

    Scanner input = new Scanner(System.in);
    System.out.print("Bitte geben sie den Wert für a ein : ");
    double a = input.nextDouble();
    System.out.print("Bitte geben sie den Wert für b ein : ");
    double b = input.nextDouble();

    if (a == 0) {
        System.out.println("x = 0");
    }
    else{
        System.out.println(-b/a);
    }
}

Upvotes: 0

ketaki shah
ketaki shah

Reputation: 36

You can use following code

import java.util.Scanner;

public class Equation {

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a Number");      
        double a = input.nextDouble();
        System.out.print("Please enter a Number ");      
        double b = input.nextDouble();

        if(a==0) {
            System.out.println("can not evaluate expression for a = 0 ");
        } else {
            double division = (b/a);
            double ans = -1 * division ;
            System.out.println(" Expression evaluation is " + ans);
        }

}

}

Upvotes: -1

c0der
c0der

Reputation: 18792

Here is a short version

    import java.util.Scanner;
    public class LinGlj4u {

        public static void main(String[] args) {

            Scanner input = new Scanner(System.in);
            System.out.print("Bitte geben sie den Wert für a ein : ");
            double  a = input.nextDouble();
            System.out.print("Bitte geben sie den Wert für b ein : ");
            double b = input.nextDouble();

            if(a != 0) {
                System.out.println("result is " +(-b/a));
            } else {
                System.out.println("result is infinity");
            }
        }

     }

Upvotes: 2

Related Questions