【发布时间】:2016-04-08 03:37:40
【问题描述】:
我的问题如下:
我有一个java课程的特定作业,其中第二类的构造函数说明如下:
定义一个构造函数,参数传入用户输入的初始位置和初始速度,并使用它们:
- 将初始位置常数和当前位置数据字段都初始化为初始位置参数的值。
- 将初始速度常数和当前速度数据字段初始化为初始速度参数的值。
现在第一个类的main方法:
定义一个主要方法来:
显示程序将对用户做什么的描述(在描述中使用高度阈值常数)。
使用静态方法(也需要提示和验证输入)从用户那里读取初始位置和初始速度值。
在读取所有用户输入后显示几个空行。 o 创建 FallingItem 类的新对象,使用从用户读取的初始位置和初始速度作为构造函数的参数。
到目前为止,我是这样设置的:
投影机类:
public static void main(String[] args) {
System.out.println("This program will calculate the position and "
+ "velocity \n of a falling object until it reaches "
+ HEIGHT_THRESHOLD + " feet above the ground.");
initialPosition();
initialVelocity();
System.out.println();
System.out.println();
FallingItem I = new FallingItem(initialPosition(), initialVelocity());
.... 方法如下:
'public static double initialPosition(){
Scanner input = new Scanner(System.in);
boolean validated; //validator for while statements
double startPosition; //variable for initialPosition
System.out.println("Please enter the intial position from which" +
" you intend to drop the object." + "this cannot be lower "+
"than 600 feet.");
do{
startPosition = input.nextDouble();
if (startPosition > HEIGHT_THRESHOLD){
validated = true;
}
else{
System.out.print("error, value is too low. Try again");
validated = false;
}
}while (validated = false);
return startPosition;
}
public static double initialVelocity(){
Scanner userInput = new Scanner(System.in);
double startVelocity;
System.out.println("\n please enter the initial velocity, in feet per" +
" second. \n This value cannot be less than -500");
startVelocity = userInput.nextDouble();
if(startVelocity <= FallingItem.TERMINAL_VELOCITY){
System.out.print("error, this value cannot be less than -500 feet" +
" per second. Please re-enter the value");
startVelocity = userInput.nextDouble();
}
return startVelocity;'
在你问之前: 是的,需要有一个 do-while 循环,并且 是的,方法需要这样提示
项目类的构造函数:
public FallingItem(double initialPosition, double initialVelocity){
INITIAL_POSITION = initialPosition; //constant to be filled by user input
INITIAL_VELOCITY = initialVelocity; //constant to be filled by user input
currentPosition = initialPosition;
currentVelocity = initialVelocity;
}
因此,简而言之,我非常感谢社区提供的任何帮助,以找到一种方法来满足要求,而不会让用户输入提示重复自己,这是目前正在发生的事情,因为坦率地说,我没有想法。
问候,火磁。
【问题讨论】:
标签: java methods constructor