英文:
Rounding does not match all expected results
问题
import java.util.Scanner;
import java.lang.Math;
public class CostEstimator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double distance = 0.0;
double fuelNeeded = 0.0;
double gallonsFuel = 0;
double costNeeded = 0.0;
final double MILES_PER_LITER = 13.7;
final double LITER_PER_GALLON = 3.785;
final double COST_PER_GALLON = 2.629;
System.out.println("输入要行驶的距离(英里):");
distance = scnr.nextDouble();
fuelNeeded = distance/13.7 ;
System.out.println("所需燃油:" + fuelNeeded + " 升");
gallonsFuel = Math.round(fuelNeeded/3.785) ;
System.out.println("所需加仑数:" + (int)gallonsFuel + " 加仑");
costNeeded = gallonsFuel * 2.629;
System.out.println("所需费用:" + costNeeded + " 美元");
}
}
输入 66
输入要行驶的距离(英里):
所需燃油:4.817518248175182 升
所需加仑数:2 加仑
所需费用:5.258 美元
输入 89
输入要行驶的距离(英里):
所需燃油:6.496350364963504 升
所需加仑数:2 加仑
所需费用:5.258 美元
输入 5000
输入要行驶的距离(英里):
所需燃油:364.9635036496351 升
所需加仑数:97 加仑
所需费用:255.013 美元
输入 1024
输入要行驶的距离(英里):
所需燃油:74.74452554744526 升
所需加仑数:20 加仑
所需费用:52.58 美元
英文:
Everything is fine with this program, it matches all the expected outputs required by my instructor except when the input is 66 and 5000
Knowing that the inputs tested were 89 , 1024 which both matched right
but when I input 66 and 5000 I always end up one gallon short ///
import java.util.Scanner;
import java.lang.Math;
public class CostEstimator {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double distance = 0.0;
double fuelNeeded = 0.0;
double gallonsFuel = 0;
double costNeeded = 0.0;
final double MILES_PER_LITER = 13.7;
final double LITER_PER_GALLON = 3.785;
final double COST_PER_GALLON = 2.629;
System.out.println("Enter the distance to be covered (miles):");
distance = scnr.nextDouble();
fuelNeeded = distance/13.7 ;
System.out.println("Fuel Needed: " + fuelNeeded + " liter(s)" );
gallonsFuel = Math.round(fuelNeeded/3.785) ;
System.out.println("Gallons needed: " + (int)gallonsFuel + " gallon(s)");
costNeeded = gallonsFuel * 2.629;
System.out.println("Cost needed: " + costNeeded + " $" );
}
}
The expected outputs for each input are such
Input 66
Enter the distance to be covered (miles):
Fuel Needed: 4.817518248175182 liter(s)
Gallons needed: 2 gallon(s)
Cost needed: 5.258 $
Input 89
Enter the distance to be covered (miles):
Fuel Needed: 6.496350364963504 liter(s)
Gallons needed: 2 gallon(s)
Cost needed: 5.258 $
Input 5000
Enter the distance to be covered (miles):
Fuel Needed: 364.9635036496351 liter(s)
Gallons needed: 97 gallon(s)
Cost needed: 255.013 $
Input 1024
Enter the distance to be covered (miles):
Fuel Needed: 74.74452554744526 liter(s)
Gallons needed: 20 gallon(s)
Cost needed: 52.58 $
答案1
得分: 2
round
函数将数字四舍五入到最近的整数,但是在购买旅程燃料时,您可能希望向上取整。请仔细检查问题的要求,是要将数字四舍五入到最近的整数还是向上取整到下一个较大的整数?
向上取整在数学中称为“ceiling”,在Java中表示为Math.ceil
。
英文:
The round
function rounds to the nearest integer, but when buying fuel for a journey you'd probably want to round up. Double-check what the question requires, whether rounding to the nearest integer or the next higher integer?
Rounding up is called "ceiling" in maths and Math.ceil
in Java.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论