Issue
I am trying to use mod to round up my lawn area and mowing time. I am stuck and my code isn`t coding lol
I tried a nested if but it wouldn't run either so I commented it out, if anyone can just fix the areas that need amending and not change the code to suit their style, but follow my way. That would help me understand my wrongs better. Thanks
package Tutorial;
import java.util.Scanner;
public class Tut3 {
public static void main(String[] args) {
double lgh = 0.0, wdt = 0.0, area = 0.0, mo = 5, mo_time = 0.0;
Scanner key = new Scanner(System.in);
System.out.print("\nPlease enter lenght in meters\t");
lgh = key.nextDouble();
System.out.print("\nPlease enter width in meters\t");
wdt = key.nextDouble();
area = lgh * wdt;
mo_time = area / mo;
//add mod to modulate any remaining numbers
if (mo_time < 60) {
System.out.printf("\nLawn area = %.2f m2\n\nMowing time %.0f mins ", area,
(mo_time % 60));
} else if (mo_time >= 60 && mo_time <= 90) {
System.out.printf("\n***Lawn area = %.2f m2\n\nMowing time %.1f hour %.0f mins ",area, (mo_time /60 ), (mo_time % 60 ));
}
// if time is >=60 use mod to round up in to hours and mins unless time is 60min then time reads 1 hour, not 1 hour 0 mins and so on, if areas sqm is 30.0 round up to 30sqm and so on
/*if (mo_time % 60 == 0) {
System.out.printf("\n###Your lawn area = %.2fm2\n\nMowing time %.1f hour", area, (mo_time / 60));
}*/
else if (mo_time > 90) {
System.out.printf("\nLawn area = %.2f m2\n\nMowing time %.0f hour %.0f mins \n\nTime to purchase a new lawnmower", area, (mo_time / 60), (mo_time % 60));
}
key.close();
}
}
Solution
Your problem is caused by this format specifier Mowing time %.0f hour
which will round of your hour value with no digits after the decimal point. For example:
lgh = 50.0
, wdt = 10.0
, So, area = 500.0
, mo_time = 500/5 = 100
.
Which will trigger the last else if case. Now, when it will try to print 100/60 = 1.6666666...
to 0 digits after decimal point. It would round up to 2
. You can use Math.floor() to get the floor value. That will be 1
in this example.
Also, in your current code, if the area
is less than mo
. It will print 0
. So, you can use Math.ceil() to take the ceiling of the minute value.
Here is the modified code.
import java.util.Scanner;
public class Tut3 {
public static void main(String[] args) {
double lgh = 0.0, wdt = 0.0, area = 0.0, mo = 5, mo_time = 0.0;
Scanner key = new Scanner(System.in);
System.out.print("\nPlease enter length in meters\t");
lgh = key.nextDouble();
System.out.print("\nPlease enter width in meters\t");
wdt = key.nextDouble();
area = lgh * wdt;
mo_time = area / mo;
//add mod to modulate any remaining numbers
if (mo_time < 60) {
System.out.printf("\nLawn area = %.2f m2\n\nMowing time %.0f mins ", area, Math.ceil(mo_time));
} else if (mo_time <= 90) {
System.out.printf("\n***Lawn area = %.2f m2\n\nMowing time %.0f hour %.0f mins ",
area, Math.floor(mo_time/60), Math.ceil(mo_time%60));
} else if (mo_time > 90) {
System.out.printf("\nLawn area = %.2f m2\n\nMowing time %.0f hour %.0f mins \n\nTime to purchase a new lawnmower",
area, Math.floor(mo_time/60), Math.ceil(mo_time%60));
}
key.close();
}
}
Hope this solves your problem.
Answered By - Rifat Rubayatul Islam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.