How to fix computation errors?
I'm trying to write a program that reads 2 numbers from the user and divides them. Here is the code I have so far:
import java.util.Scanner;
public class divideByZero {
public static int quotient(int numerator, int denominator)
{
return numerator / denominator;
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the first number: ");
int numerator = scanner.nextInt();
System.out.print("Please enter the second number: ");
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
float result2 = (float)result;
System.out.printf("\n The first number %d divided by the second number "
+ "%d = %f\n", numerator, denominator, result2 );
}
I'm having problems with the computat开发者_Python百科ions. For example, when I enter 3 divided by 4, I get the result 0.000000. How do I get the correct result to 2 decimal places?
Cast the numerator and denominator as floats before you divide them.
import java.util.Scanner;
public class divideByZero {
public static float quotient(float numerator, float denominator)
{
return numerator / denominator;
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the first number: ");
int numerator = scanner.nextInt();
System.out.print("Please enter the second number: ");
int denominator = scanner.nextInt();
float result = quotient( (float) numerator, (float) denominator );
System.out.printf("\n The first number %d divided by the second number "
+ "%d = %f\n", numerator, denominator, result );
}
The following changes will do what you need.
public static float quotient(int numerator, int denominator)
{
return (float)numerator / (float)denominator;
}
float result = quotient( numerator, denominator );
System.out.printf("\n The first number %d divided by the second number "
+ "%d = %.2f\n", numerator, denominator, result2 );
You cannot do division on an int and get a fractional int (there is no such thing), so the division must be done with floats and stored into a float. The %.2f limits it to 2 decimal places.
Why don't you use the datatype double instead of float?
Here is some code to format into two decimal points:
import java.text.*;
public class DecimalPlaces {
public static void main(String[] args) {
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
}
}
You are getting 0.0000 because you are dividing integer numbers and storing that result back into an integer. To see the fractional part, you can declare result to be a float variable.
When that's working, to print 2 decimals you can use printf formatting.
In C it'd be something like %.2f.
Based on the errors, I think you'd benefit from reading about data types, especially floating point numbers.
If you change the return type of the quotient method from an int to a float, it will no longer round the answer to the nearest integer.
public static float quotient(int numerator, int denominator)
{
return (float) numerator / (float) denominator;
}
精彩评论