英文:
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("Please enter vehicle speed: ");
scanner.nextDouble();
speed.setSpeed(scanner.nextDouble());
System.out.println("Please enther hours vehicle was in motion: ");
scanner.nextInt();
hours.setHours(scanner.nextInt());
System.out.println("Hour\tDistance Travelled\n----\t----------");
for (int hours = 1; hours <= distance; hours++) {
}
}
}
答案1
得分: 3
你用名称 hours
定义了两个变量:
- 这里:
Distance hours = new Distance();
- 以及这里:
for (int hours = 1; hours <= distancel; hours++)
关于 <=
:你试图比较复杂对象,因为 hours
(第一个定义)和 distance
都是 Distance
类的实例。因此出现了这两个错误。
英文:
Well, you defined two variables with the name hours
:
- here:
Distance hours = new Distance();
- and here:
for (int hours = 1; hours <= distancel; hours++)
And about the <=
: 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
函数需要更改,以根据 speed
和 time
返回距离,而不需要将距离作为参数传递给它。
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 <=
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("Please enter vehicle speed: ");
distance.setSpeed(scanner.nextDouble());
System.out.print("Please enter hours vehicle was in motion: ");
distance.setHours(scanner.nextInt());
// Display the result
System.out.printf("Hour: %d, Speed: %f, Distance Travelled: %f", 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 <=
numerical operator. Both of this operator's operands must be primitives, or anything that can be unboxed to the primitives.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论