在Java中使用舍入

时间:2019-11-09 02:42:33

标签: java double rounding

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

    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秒)

1 个答案:

答案 0 :(得分:0)

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

更改:

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