首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何调用此Java代码中的方法和修复错误?

如何调用此Java代码中的方法和修复错误?
EN

Stack Overflow用户
提问于 2021-07-19 05:49:10
回答 2查看 534关注 0票数 1

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

这里是我正在练习的问题-

求线的总长度之和。一条线被定义为数字线上的两个点A和B。例如,如果A= -3,B= 10,线的长度是13。这是因为数字线上的-3到10之间的距离是13个单位(10 -(-3) = 13)。同样地,如果A=9,B=5时,线的长度应该是4个单位,而数字线上的距离是4个单位(9-5= 4)。

输入-将有2行,每一行将有数字A&B整数分隔的空间。

输出-这应该返回由用户输入的2行的长度之和。

样本输入:

代码语言:javascript
复制
5 9
-10 3

预期产出:

代码语言:javascript
复制
17

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

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

我的代码-

代码语言:javascript
复制
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;
        }
    }
}

编译时间错误-

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2021-07-19 06:21:10

尝尝这个

代码语言:javascript
复制
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;
         
}
票数 0
EN

Stack Overflow用户

发布于 2021-07-25 14:24:32

私有静态int getTotalSumOfLines(Line firstLine,Line secondLine) {

代码语言:javascript
复制
     // take this to getTotalSumOfLines

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

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68435623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档