Why does (360 / 24) / 60 = 0 ... in Java
None of the operands in the arithmetic is a float - so it's all being done with integer arithmetic and then converted to a float. If you change the type of an appropriate operand to a float, it'll work fine:
float div = ((360 / 24f) / 60); // div is now 0.25
Note that if you changed just 60 to be a float, you'd end up with the 360 / 24 being performed as integer arithmetic - which is fine in this particular case, but doesn't represent what I suspect you really intended. Basically you need to make sure that arithmetic operation is being performed in the way that you want.
Java float not acting correctly
115 and 100 are both integers, so will return an integer.
Try doing this:
float rate = (115f / 100f);
Find angle between hour and minute hands in an analog clock
It turns out that Wikipedia does have the best answer:
// h = 1..12, m = 0..59
static double angle(int h, int m) {
double hAngle = 0.5D * (h * 60 + m);
double mAngle = 6 * m;
double angle = Math.abs(hAngle - mAngle);
angle = Math.min(angle, 360 - angle);
return angle;
}
Basically:
- The hour hand moves at the rate of
0.5
degrees per minute - The minute hand moves at the rate of of
6
degrees per minute
Problem solved.
And precision isn't a concern because the fractional part is either .0
or .5
, and in the range of 0..360
, all of these values are exactly representable in double
.
division problems
Are you sure that your array is not integer ?
if it's, try using double.
for loop structure
You should probably put the if-statment outside the for-loop. That way you don't need the if-statement. Second the statement in the loop should be < 4 instead of <= 4.
public class ExamsFor4 {
public static void main(String[] arguments) {
int inputNumber; // One of the exams input by the user.
int sum = 0; // The sum of the exams.
int i; // Number of exams.
Double Avg; // The average of the exams.
TextIO.put("Please enter the first exam: "); // get the first exam.
inputNumber = TextIO.getlnInt();
for ( i = 1; i < 4; i++ ) {
sum += inputNumber; // Add inputNumber to running sum.
TextIO.put("Please enter the next exam: "); // get the next exam.
inputNumber = TextIO.getlnInt();
}
Avg = ((double)sum) / i;
TextIO.putln();
TextIO.putln("The total sum for all " + i +" exams is " + sum);
TextIO.putf("The average for the exams entered is %1.2f.\n", Avg);
break;
} // end main ()
}
Related Topics
"Loading Class Com.Mysql.Jdbc.Driver ... Is Deprecated" Message
Where Is the Documentation for the Values() Method of Enum
How to Properly Express Jpql "Join Fetch" with "Where" Clause as JPA 2 Criteriaquery
How to Find All the Methods That Call a Given Method in Java
How to Read the Manifest File for a Webapp Running in Apache Tomcat
Load Resource from Anywhere in Classpath
How to Initialize Hashset Values by Construction
How to Initialize an Arraylist with All Zeroes in Java
Can One Do a for Each Loop in Java in Reverse Order
Httpurlconnection Invalid Http Method: Patch
Why Does (360/24)/60 = 0 ... in Java
Bigdecimal Equals() Versus Compareto()
Setting Jvm Heap Size at Runtime
Difference Between Arrays.Aslist(Array) and New Arraylist<Integer>(Arrays.Aslist(Array))