I am trying to write a program that will take two user input numbers and then perform calculations based off of them, but I want to use if-statements to check if it is trying to divide by zero or the output will be infinity.
import java.util.Scanner;
public class work {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double a, b, c, d;
System.out.printf("Enter the first number:");
a = scan.nextDouble();
System.out.printf("Enter the second number:");
b = scan.nextDouble();
/* various calculations */
c = a / b;
d = b / a;
if (a > 0)
{
System.out.printf("a/b = %.2f\n", c);
}
if (b > 0)
{ System.out.printf("b/a = %.2f\n", d);
}
else if (a <= 0)
{ System.out.printf("a/b = %.1f\n", d);
if (b > 0)
System.out.printf("a/b = INF\n");
}
}
}
So for example if I input 4 and 5 it will end up like this:
Enter the first number: 4
Enter the second number: 5
a/b = 0.80
b/a = 1.25
However I am having trouble getting it to check for zero, and end up with lots of weird outputs. How can I get outputs like this?
------ Sample run 2:
Enter the first number: 0
Enter the second number: 4
a/b = 0.0
b/a = INF
------ Sample run 3:
Enter the first number: 4
Enter the second number: 0
a/b = INF
b/a = 0.0
------ Sample run 4:
Enter the first number: 0
Enter the second number: 0
a/b = INF
b/a = INF
Seems you have multiple problems here. Your first problem here is that you're checking the numerator when deciding to whether or the solution is INF. For example, if you input 1 and 0, your code checks if a > 0 (which it is) and outputs c which is 1/0. What you really want is to do is check if the dominator of the equation (in this case b) is equal to zero.
It seems that you also forgot an else statement on the first if and I'm not sure what you were trying to accomplish in the else in the second if statement.
Your third problem is that your code is checking if the variable is less than 0, instead of not equal to 0, which will create unexpected results with any negative inputs. Remember that only zero will cause the answer to be undefined, or INF as you call it. In any case, the code below should function as intended. Note that I've slightly modified the class name to comply with Java naming conventions.
import java.util.Scanner;
public class Work {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double a, b, c, d;
System.out.print("Enter the first number: ");
a = scan.nextDouble();
System.out.print("Enter the second number: ");
b = scan.nextDouble();
/* various calculations */
c = a / b;
d = b / a;
if (b != 0)
{
System.out.printf("a/b = %.2f\n", c); /* the dominator (b) is not
zero, so the solution is a/b
(stored in the variable c) */
}
else
{
System.out.print("a/b = INF\n"); /* the dominator (b) is zero,
so the solution is INF */
}
if (a != 0)
{
System.out.print("b/a = %.2f\n", d); /* the dominator (a) is not
zero, so the solution is a/b
(stored in the variable d) */
}
else
{
System.out.printf("b/a = INF\n"); /* the dominator (a) is zero,
so the solution is INF */
}
}
}