运算符“<=”在参数类型int、Distance上未定义,以及更多错误。

huangapple go评论75阅读模式
英文:

The operator <= is undefined for the argument type int, Distance and more errors

问题

我认为我在 Distance.java 代码部分写得没问题。只是在尝试从 Driver.java 显示循环输出时遇到了问题。

问题是它显示了 "the operator <= is undefined for the argument type int, Distance" 和 "hours" 是重复的局部变量。

"'the operator <= is undefined for the argument type int, Distance'" 可能是因为 "distance.getDistance(distance);" 被显示为字符串。但我该如何将其变为双精度?以及我如何解决 "hours" 问题。

```java
public class Distance {

	// 创建私有属性
	private double speed;
	private int hours;

	// 创建 Distance 方法
	public Distance() {

		// 创建 getter 和 setter
	}

	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public int getHours() {
		return hours;
	}

	public void setHours(int hours) {
		this.hours = hours;
	}

	// 设置距离计算公式
	public double getDistance(double distance) {
		return distance = hours * speed;
	}
}
import java.util.Scanner;

public class Driver {

	public static void main(String[] args) {
		// TODO: 自动生成的方法存根

		// 设置一个扫描器
		Scanner scanner = new Scanner(System.in);

		// 创建对象
		Distance speed = new Distance();
		Distance hours = new Distance();
		Distance distance = new Distance();

		distance.getDistance(distance);

		// 输入车速和小时数
		System.out.println("请输入车速:");
		scanner.nextDouble();
		speed.setSpeed(scanner.nextDouble());

		System.out.println("请输入车辆运行时间:");
		scanner.nextInt();
		hours.setHours(scanner.nextInt());

		System.out.println("小时\t行驶距离\n----\t----------");
		for (int hours = 1; hours <= distance; hours++) {

		}
	}
}
英文:

I think I got the codes right on the Distance.java. Just stuck at trying to show the loop output from the Driver.java.

The problem is that it says "the operator <= is undefined for the argument type int, Distance" and "hours" is a duplicate local variable.

"The operator <= is undefined for the argument type int, Distance" is probably due to the fact that "distance.getDistance(distance);" being shown as a string. but how would I make it a double? And how would I solve the "hours" problem.

public class Distance {

	// creating private statement
	private double speed;
	private int hours;

	// creating a Distance method
	public Distance() {

		// creating getters and setters
	}

	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public int getHours() {
		return hours;
	}

	public void setHours(int hours) {
		this.hours = hours;
	}

	// setting up distance equation
	public double getDistance(double distance) {
		return distance = hours * speed;
	}
}
import java.util.Scanner;

public class Driver {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// setting up a scanner
		Scanner scanner = new Scanner(System.in);

		// creating objects
		Distance speed = new Distance();
		Distance hours = new Distance();
		Distance distance = new Distance();

		distance.getDistance(distance);

		// input for vehicle speed and hour
		System.out.println(&quot;Please enter vehicle speed: &quot;);
		scanner.nextDouble();
		speed.setSpeed(scanner.nextDouble());

		System.out.println(&quot;Please enther hours vehicle was in motion: &quot;);
		scanner.nextInt();
		hours.setHours(scanner.nextInt());

		System.out.println(&quot;Hour\tDistance Travelled\n----\t----------&quot;);
		for (int hours = 1; hours &lt;= distance; hours++) {

		}
	}
}

答案1

得分: 3

你用名称 hours 定义了两个变量:

  • 这里:Distance hours = new Distance();
  • 以及这里:for (int hours = 1; hours &lt;= distancel; hours++)

关于 &lt;=:你试图比较复杂对象,因为 hours(第一个定义)和 distance 都是 Distance 类的实例。因此出现了这两个错误。

英文:

Well, you defined two variables with the name hours:

  • here: Distance hours = new Distance();
  • and here: for (int hours = 1; hours &lt;= distancel; hours++)

And about the &lt;=: you are trying to compare complex objects, since hours (the first definition) and distance are instances of Distance class. Therefore, the two errors.

答案2

得分: 1

操作符<=应该与基本类型(例如 int、long、double 等)或其包装类型(例如 Integer、Long、Double 等)一起使用,而不适用于其他类型。

只要输入预计用于单个对象,您无需为每个输入创建那么多对象。另外,getDistance 函数需要更改,以根据 speedtime 返回距离,而不需要将距离作为参数传递给它。

import java.util.Scanner;

class Distance {
    private double speed;
    private int hours;

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public int getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    public double getDistance() {
        return hours * speed;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 创建 Distance 对象
        Distance distance = new Distance();

        // 输入车速和时间
        System.out.print("请输入车速:");
        distance.setSpeed(scanner.nextDouble());

        System.out.print("请输入车辆运行时间:");
        distance.setHours(scanner.nextInt());

        // 显示结果
        System.out.printf("时间:%d 小时,速度:%f,行驶距离:%f", distance.getHours(), distance.getSpeed(),
                distance.getDistance());
    }
}

一个示例运行:

请输入车速:50.5
请输入车辆运行时间:5
时间:5 小时,速度:50.500000,行驶距离:252.500000

英文:

The operator &lt;= is meant to be used with the primitive types (e.g. int, long, double etc.) or their boxed types (e.g. Integer, Long, Double etc.), not for other types.

You do not need to create as many objects as for each input as long as the inputs are supposed to be for just one object. Also, the getDistance function needs to be changed to return the distance based on speed and time without requiring the distance to be passed to it as a parameter.

import java.util.Scanner;

class Distance {
	private double speed;
	private int hours;

	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public int getHours() {
		return hours;
	}

	public void setHours(int hours) {
		this.hours = hours;
	}

	public double getDistance() {
		return hours * speed;
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		// Creating an object of Distance
		Distance distance = new Distance();

		// Input for vehicle speed and hour
		System.out.print(&quot;Please enter vehicle speed: &quot;);
		distance.setSpeed(scanner.nextDouble());

		System.out.print(&quot;Please enter hours vehicle was in motion: &quot;);
		distance.setHours(scanner.nextInt());

		// Display the result
		System.out.printf(&quot;Hour: %d, Speed: %f, Distance Travelled: %f&quot;, distance.getHours(), distance.getSpeed(),
				distance.getDistance());
	}
}

A sample run:

Please enter vehicle speed: 50.5
Please enter hours vehicle was in motion: 5
Hour: 5, Speed: 50.500000, Distance Travelled: 252.500000

答案3

得分: 0

请看JLS §15.20.1

数值比较运算符的每个操作数的类型必须是可转换(§5.1.8)为原始数值类型的类型,否则会导致编译时错误。

你的distance是一个引用类型变量,不能作为<=数值运算符的操作数。这个运算符的两个操作数都必须是原始类型,或者是可以拆箱为原始类型的任何内容。

英文:

Have a look at JLS §15.20.1:
>The type of each of the operands of a numerical comparison operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs.

Your distance is a reference type variable, that cannot be an operand used with a &lt;= numerical operator. Both of this operator's operands must be primitives, or anything that can be unboxed to the primitives.

huangapple
  • 本文由 发表于 2020年10月17日 06:08:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64397007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定