【发布时间】:2020-03-05 15:23:24
【问题描述】:
我知道有一种方法可以使我的程序不四舍五入,但我想不通,而且每次尝试它都会向我抛出不同的错误。所以我的代码减去所有四舍五入的尝试是
public class TheoryTest {
public static void main (String[] args) {
System.out.printf("%-15s%-15s%5s%-15s%-15s\n", "Feet", "Meter", "| ", "Meter", "Feet");
System.out.println("----------------------------------------------------------");
double foot = 1.0; double meter = 20.0;
for (int i = 1; i <= 10; foot++, meter += 5, i++) {
System.out.printf("%-15.1f%-15.3f%5s%-15.1f%-15.3f\n", foot, footToMeter(foot), "| ", meter,
meterToFoot(meter));
}
}
public static double footToMeter(double foot) {
Math.floor(return 0.305*foot);
}
public static double meterToFoot(double meter) {
Math.floor(return);
return 3.2787*meter;
}
}
错误:表达式的非法开始 Math.floor(return 0.305*foot);
错误:';'预期的 Math.floor(return 0.305*foot);
错误:表达式的非法开始 Math.floor(return 0.305*foot);
错误:';'预期的 Math.floor(return 0.305*foot);
错误:表达式的非法开始 Math.floor(return);
错误:';'预期的 Math.floor(return);
6 个错误 C:\Users\Shannon Reynolds\Desktop\try\LabChapter69\nbproject\build-impl.xml:930:执行此行时出现以下错误:
C:\Users\Shannon Reynolds\Desktop\try\LabChapter69\nbproject\build-impl.xml:270: 编译失败;有关详细信息,请参阅编译器错误输出。 构建失败(总时间:1 秒)
【问题讨论】:
-
当您说向下取整时,您是指 Math.floor(double a) 吗?给我们一个例子,包括输出和预期输出。只需编辑您的问题。
-
我目前看到的唯一舍入来自格式化程序语句,它们应该按照您的预期进行舍入。
-
double myVal = BigDecimal.valueOf(doubleValueToRound).setScale(intPrecisionYouWant, RoundingMode.DOWN).doubleValue(); -
为什么要把“return”放在括号里?
-
你总是可以转换为 int 并返回到 double ,它的效果与
Math.floor()