找出总距离

时间:2019-05-22 19:32:56

标签: java

我有一个Java代码用于将两个给定距离(英尺,英寸)相加。我想找出两点之间的总距离。

我有一个代码,想要对两个距离求和并找出总距离。但是这段代码无法提供我想要的答案。

import java.util.Scanner;

public class Distance {
    Scanner input=new Scanner(System.in);
    int feet;int inches;
    public void setDistance()
    {
        System.out.print("enter feets:");
        int f=input.nextInt();
        System.out.print("Enter inches :");
        int i=input.nextInt();
    }
    public void addDistance(Distance d1,Distance d2)
    {
        feet=d1.feet+d2.feet+(inches/12);
        inches=d1.inches+d2.inches;
        inches=inches%12;
    }
    public void display()
    {
        System.out.println("feets :"+feet+"\tinches :"+inches);
    }
}
public class Home_Actvity3 {

    public static void main(String[] args) {
        Distance d1=new Distance();
        Distance d2=new Distance();
        Distance d3=new Distance();
        System.out.println("Enter first distance :");
        d1.setDistance();
        System.out.println("Enter second distance :");
        d2.setDistance();
        System.out.println("Total distance :");
        d1.addDistance(d1, d2);
        d3.display();

    }

}

当我输入这些值作为输入时。我是我想要的答案。

run:
Enter first distance :
enter feets:2
Enter inches :5
Enter second distance :
enter feets:3
Enter inches :7
Total distance :
feets :0    inches :0
BUILD SUCCESSFUL (total time: 24 seconds)

我想要那个答案

total distance:
feets :6        inches :0

1 个答案:

答案 0 :(得分:0)

您的代码片段:

System.out.print("enter feets:");
int f=input.nextInt();
System.out.print("Enter inches :");
int i=input.nextInt();

变量i和f不是类成员。

System.out.print("enter feets:");
int f=input.nextInt();
feet = f;
System.out.print("Enter inches :");
int i=input.nextInt();
inches = i;

是的!