gpt4 book ai didi

java - 新对象参数/对象构造函数设置

转载 作者:行者123 更新时间:2023-12-01 10:25:11 24 4
gpt4 key购买 nike

所以,我试图弄清楚如何正确设置我的新对象参数以及我的对象构造函数参数。当我按原样保留新对象行时,我收到错误消息“类 KingFallingItem 中的构造函数 KingFallingItem 无法应用于给定类型;要求:双,双发现:没有参数原因:实际参数列表和正式参数列表的长度不同”

我不太确定应该在新对象创建行的 () 中放置什么内容。我尝试了双initialPosition和initialVelocity,以及getInitPosition和getInitVelocity。我觉得我在这里忘记了一些重要的信息。任何帮助,将不胜感激。我还没有完成程序所有部分的编码,因此 for 循环没有任何意义,我想首先让第一部分全部正常工作。

第一个文件:

public class KingFallingItem {

//data fields
private final double INITIAL_POSITION;
private final double INITIAL_VELOCITY;
private int currentTime;
private double currentPosition;
private double currentVelocity;
public static final int TERMINAL_VELOCITY = -500;

// box contstructor
public KingFallingItem(double initialPosition, double initialVelocity) {
INITIAL_POSITION = initialPosition;
INITIAL_VELOCITY = initialVelocity;
currentTime = 0;
currentPosition = INITIAL_POSITION;
currentVelocity = INITIAL_VELOCITY;
}
}

第二个文件:

import java.util.Scanner;

public class KingTrajectoryProjector {
public static int HEIGHT_THRESHOLD = 600;

public static void main(String[] args) {
System.out.println("This program will calculate the position and"
+ "velocity of a alling object until it reaches "
+ HEIGHT_THRESHOLD + " feet above ground");

getInitPosition();
getInitVelocity();
System.out.println();
System.out.println();

KingFallingItem fallingItem = new KingFallingItem();


int count;
for (count = 5; count >= 1; count--) {
System.out.println("Countdown:");
System.out.println(" " + count);
}
}

public static double getInitPosition() {
Scanner keyboard = new Scanner(System.in);
double initialPosition;

do {
System.out.print("Enter an initial position"
+ " (must be over 600.0 feet): ");
initialPosition = keyboard.nextDouble();

} while (initialPosition < HEIGHT_THRESHOLD);
return initialPosition;
}

public static double getInitVelocity() {
Scanner keyboard = new Scanner(System.in);
double initialVelocity;

do {
System.out.print("Enter an initial velocity "
+ "(-500.0 ft/sec or more: ");
initialVelocity = keyboard.nextDouble();
} while (initialVelocity < -500.0);
return initialVelocity;
}

}

最佳答案

我不明白为什么这行不通

double position = getInitPosition();
double velocity = getInitVelocity();
System.out.println();
System.out.println();

KingFallingItem fallingItem = new KingFallingItem(position, velocity);

关于java - 新对象参数/对象构造函数设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35386757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com