-3

我有一项任务要编写一个程序,该程序将采用用户输入的速度和小时数来查找距离。我有一个带有计算方法的类,但需要使用 for 循环来输出一个表格,该表格将显示用户输入的每个小时的旅行小时数,如下所示:

小时距离

1 10 2 20 3 30 等

首先,我编写了类来进行计算,但是当我将 for 循环放在一起时,计数器出现错误。

这是具有计算距离的方法的对象类。

package distancetraveled;

public class DistanceTraveled {

private double speed;       //speed
private int hours;      //hours
private double distance;    //distance

//distanceTravelled constructor
public DistanceTraveled(double s, int h) {
    speed = s;
    hours = h;
}

// findDistance method
public double findDistance() {
    distance = speed * hours;
    return distance;
}

// setSpeed method
public void setSpeed(double s) {
    s = speed;
}

// set distance method
public void setDistance(int h) {
    h = hours;
}

// getSpeed method
public double getSpeed() {
    return speed;
}

//getDistance method
public double getDistance() {
    return distance;
}

}

以及显示表格的程序:

package distancetraveled;

import java.util.Scanner;

public class DistanceTraveledTest {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    double testSpeed;
    int testHours;

    System.out.println("What is the speed of the vehicle?");
    testSpeed = input.nextDouble();
    while (testSpeed <= 1){
        System.out.print("The speed must be equal to or greater than one");
        System.out.println("What is the speed of the vehicle?");
        testSpeed = input.nextDouble();
    }

    System.out.println("How many hours has the vehicle been travelling?");
    testHours = input.nextInt();

    while (testHours <= 1){
        System.out.print("The hours must be equal to or greater than one");
        System.out.println("How many hours has the vehicle been traveling?");
        testHours = input.nextInt();
    }


    DistanceTraveled traveller = new DistanceTraveled(testSpeed, testHours);

    System.out.println("Hour\t\tDistance Travelled");
    System.out.println("------------------------------");

    for(i=0;i<=testHours;i++){
           System.out.print(testHours + "\t\t" + traveller.findDistance());



    }

}
}

我敢肯定还有很多其他错误。谢谢大家和任何人。

4

3 回答 3

2

i您没有在循环中声明变量...

for (i = 0; i <= testHours; i++) {

它应该看起来更像...

for (int i = 0; i <= testHours; i++) {

更新

您的setDistance方法也没有正确分配值...

public void setDistance(int h) {
    h = hours;
}

应该...

public void setDistance(int h) {
    hours = h;
}

(我相信克里斯托弗已经解决了这个问题)

您的DistanceTraveled班级似乎需要比实际需要的更多信息。因为时间是可变的,我会摆脱它......

相反,我会使用类似...

public class DistanceTraveled {

    private double speed;       //speed

    //distanceTravelled constructor
    public DistanceTraveled(double s) {
        speed = s;
    }

    // findDistanceOverTime method
    public double findDistanceOverTime(int hours) {
        return getSpeed() * hours;
    }

    // setSpeed method
    public void setSpeed(double s) {
        s = speed;
    }

    // getSpeed method
    public double getSpeed() {
        return speed;
    }
}

这会产生一个看起来更像...的循环

for (int i = 0; i <= testHours; i++) {
    System.out.println(i + "\t\t" + traveller.findDistanceOverTime(i));
}

这让我开始质疑这个DistanceTraveled类的有效性,因为它可以很容易地用static方法来完成......

public class DistanceTraveled {
    public static double findDistanceOverTime(double speed, int hours) {
        return speed * hours;
    }
}

产生一个更像...的循环

for (int i = 0; i <= testHours; i++) {
    System.out.println(i + "\t\t" + DistanceTraveled.findDistanceOverTime(testSpeed, i));
}

当然,这都是猜测,取决于您的整体要求......

于 2013-04-16T01:04:24.477 回答
1

你的错误在DistanceTraveledTest课堂上。

您的for循环有一个未声明的int类型变量:

for(i=0;i<=testHours;i++){
    System.out.print(testHours + "\t\t" + traveller.findDistance());
}

你可以这样做:

for(int i=0;i<=testHours;i++){
    System.out.print(testHours + "\t\t" + traveller.findDistance());
}

或者:

int i = 0;
for(i=0;i<=testHours;i++){
    System.out.print(testHours + "\t\t" + traveller.findDistance());
}

另外,在你的DistanceTraveled课堂上,你应该考虑this为你的 getter 和 setter 使用关键字,而且我认为你有一个左手分配问题:

//distanceTravelled constructor
public DistanceTraveled(double s, int h) {
    this.speed = s;
    this.hours = h;
}

// findDistance method
public double findDistance() {
    this.distance = this.speed * this.hours;
    return this.distance;
}

/****************************************************
 * This is where your assignment issue is
 * You are reassigning the passed method variable to the
 * class assigned this.speed
 ****************************************************/
// setSpeed method
public void setSpeed(double s) {
    s = speed;
}

// set distance method
public void setDistance(int h) {
    h = hours;
}

/*****************************************************
 * These and other methods should be like this:
 *****************************************************/
public void setSpeed(double s) {
    this.speed = s;
}

// set distance method
public void setDistance(int h) {
    this.hours = h;
}

// getSpeed method
public double getSpeed() {
    return this.speed;
}

//getDistance method
public double getDistance() {
    return this.distance;
}
于 2013-04-16T01:11:22.840 回答
0
for(int i=0;i<=testHours;i++)

或者你可以

int i = 0;
for(;i<=testHours;i++)
于 2013-04-16T01:34:37.663 回答