试图使用for循环来制作一个表,得到一个"找不到符号错误"

时间:2013-04-16 01:01:08

标签: java

我有一个分配来制作一个程序,它将用户输入速度和小时来查找距离。我有一个类,有一个方法来进行计算,但需要使用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());



    }

}
}

我确信还有很多其他错误。谢谢大家和任何人。

3 个答案:

答案 0 :(得分: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));
}

当然,这是所有猜想,取决于您的整体要求......

答案 1 :(得分: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课程中,您应该考虑为您的getter和setter使用this关键字,而且我认为您有左手工作分配问题:

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

答案 2 :(得分:0)

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

或者你可以

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