【问题标题】:How to call the method & fix error in this Java code?如何调用此 Java 代码中的方法并修复错误?
【发布时间】:2021-09-26 19:54:45
【问题描述】:

我是 Java 新手,我试图让这段代码运行,但似乎总是给我错误。 我也尝试使用 sum 和其他函数。我做错了什么?

这是我正在练习的问题-

求行长的总和。一条线由数轴上的 2 个点 A 和 B 定义。例如,如果 A = -3 且 B = 10,则线的长度为 13。这是因为数轴上 -3 和 10 之间的距离为 13 个单位 (10 -(-3) = 13) 同样,如果 A = 9 和 B = 5,则线的长度应为 4 个单位,因为数字轴上 9 和 5 之间的距离为 4 个单位(9 - 5 = 4)

输入 - 将有 2 行,每行将有数字 A 和 B 整数,以空格分隔。

输出 - 这应该返回用户输入的两行长度的总和。

示例输入:

5 9
-10 3

预期输出:

17

说明: 第一行表示第一行的坐标,即 A = 5,B = 9。 第二行代表第二行的坐标,即A = -10,B = 3。

第一行长度 = 4,第二行长度为 13。因此输出为 17。

我的代码-

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Source{


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] firstLineCoordinates = br.readLine().split(" ");
        int a1 = Integer.parseInt(firstLineCoordinates[0]);
        int b1 = Integer.parseInt(firstLineCoordinates[1]);

String[] secondLineCoordinates = br.readLine().split(" ");
        int a2 = Integer.parseInt(secondLineCoordinates[0]);
        int b2 = Integer.parseInt(secondLineCoordinates[1]);

        Line firstLine = new Line(a1, b1);
        Line secondLine = new Line(a2, b2);

        int totalSumOfLines = getTotalSumOfLines(firstLine, secondLine);
        System.out.println(totalSumOfLines);

        br.close();
    }

    private static int getTotalSumOfLines(Line firstLine, Line secondLine) {
        int sum = totalSumOfLines;
        return sum((b2 - a2) + (b1 - a1));
         // ERROR IN THIS METHOD
         
    }

    public static class Line {
        private int a;
        private int b;
public Line(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int getA() {
            return a;
        }

        public int getB() {
            return b;
        }
    }
}

我得到的编译时错误-

Source.java:29: error: cannot find symbol
        int sum = totalSumOfLines;
                  ^
  symbol:   variable totalSumOfLines
  location: class Source
Source.java:30: error: cannot find symbol
        return sum((b2 - a2) + (b1 - a1));
                    ^
  symbol:   variable b2
  location: class Source
Source.java:30: error: cannot find symbol
        return sum((b2 - a2) + (b1 - a1));
                         ^
  symbol:   variable a2
  location: class Source
Source.java:30: error: cannot find symbol
        return sum((b2 - a2) + (b1 - a1));
                                ^
  symbol:   variable b1
  location: class Source
Source.java:30: error: cannot find symbol
        return sum((b2 - a2) + (b1 - a1));
                                 ^
symbol:   variable a1
  location: class Source
5 errors

【问题讨论】:

  • main() 中定义的变量在getTotalSumOfLines() 方法的范围内均不可见。请参阅What is 'scope' in Java? 将变量设为类级别或将变量传递给您需要它们的方法。
  • 问题在于变量的范围。

标签: java error-handling compiler-errors sum


【解决方案1】:

private static int getTotalSumOfLines(Line firstLine, Line secondLine) {

     // take this to getTotalSumOfLines

    int sum1= Math.abs((firstLine.getA() - firstLine.getB()));
    int sum2= Math.abs((secondLine.getA() - secondLine.getB()));
    return sum1+sum2;

}

【讨论】:

    【解决方案2】:

    试试这个

    private static int getTotalSumOfLines(Line firstLine, Line secondLine) {
        int sum = 0;
        if(firstLine.getA() > firstLine.getB())
            sum = sum + (firstLine.getA() - firstLine.getB());
        else
            sum = sum + (firstLine.getB() - firstLine.getA());
        if(SecondLine.getA() > secondLine.getB())
            sum = sum + (SecondLine.getA() - secondLine.getB());
        else
            sum = sum + (secondLine.getB() - SecondLine.getA());
        return sum;
             
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2019-11-23
      • 1970-01-01
      相关资源
      最近更新 更多