在 Java 中使用舍入

Using rounddown in Java

我知道有一种方法可以使我的程序不向上舍入,但我想不出来,每次我尝试它都会向我抛出不同的错误。所以我的代码减去所有四舍五入的尝试是

    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*英尺);

错误:“;”预期的 Math.floor(return 0.305*英尺);

错误:表达式的开头非法 Math.floor(return 0.305*英尺);

错误:“;”预期的 Math.floor(return 0.305*英尺);

错误:表达式的开头非法 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 秒)

语句已关闭... return double 的函数应该 return 一个值,而不是参数列表中的 return。

变化:

  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;
  }

...至:

  public static double footToMeter(double foot) {
    return Math.floor(0.305*foot);
  }

  public static double meterToFoot(double meter) { 
    return 3.2787*meter;
  }