我有一个项目,我们使用了 Throttle 类和 MainClass。我们现在必须创建一个新的 while 循环,提示用户使用扫描仪输入从 0 到 6 的位置数据。
当用户输入-1时,代码需要退出。我只是在实现具有节流级别的扫描仪时遇到问题。我可以不使用 Throttle 类,但这不是作业。
完整说明(复制和粘贴):
- 通过删除 while 循环来修改 MainClass。
- 创建一个新的 while 循环,提示用户使用扫描仪输入位置数据。
- 当用户输入-1时,代码将退出。
用户应输入0到6之间的位置。除-1外,数据必须在0到6之间;所有其他数据均应被忽略。用户每次输入有效位置,流量都会重新计算并显示。
这是主类:
package Pck1;
public class MainClass {
public static void main(String[] args) {
int position = 3;
//position is used in-case of using something different from 3
Throttle throttle = new Throttle(position);
while (throttle.is_on())
{
System.out.println("The throttle is on." + "flow = "
+ throttle.flow());
throttle.shift(-1);
}
System.out.println("The throttle is off." );
}
}
这里是油门类别:
package Pck1;
public class Throttle {
// Data Members
private int position;
// Constructors
public Throttle(int position) {
this.position = position;
}
// Membership Functions
public boolean is_on() {
return flow() > 0;
}
public double flow() {
return position / 6.0;
}
public void shift(int amount) {
position += amount;
if (position < 0)
position = 0;
else if (position > 6)
position = 6;
}
public void shut_off() {
position = 0;
}
}
我尝试了以下方法,但我知道它很糟糕。
package Pck1;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
int j = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter speed between 0 and 6: ");
j = input.nextInt();
Throttle throttle = new Throttle(j);
while(j > 6){
j = input.nextInt();//read input from user
System.out.println();
if (j >=0 && j <=6 ) {
System.out.println("Validated input: " + j);
input.close();
}//end if statement
else {
System.out.println("Enter a number between 1 and 6");
j = input.nextInt();
}//end else statement
}//end while loop
}//end main method
}//end class
需要注意的是,我对 Java 的经验并不丰富,这里有一个满足说明的选项(带有一些注释来解释实现)。
从根本上来说,我认为这个分配是错误的:你的
Throttle
类没有方法直接操纵油门位置;您只能向上或向下移动相对数量。提示用户输入正数或负数,然后将其提供给 Throttle.shift
方法会更有意义;或者,如果 Throttle
类具有类似 setPosition
方法之类的东西,则此分配也会更明智。事实并非如此,这意味着完成指令的唯一方法是创建一个新的 Throttle
实例来响应用户的每个输入。这完全是废话。
无论如何,给你:
package Pck1;
import java.util.Scanner;
public class MainClass {
/**
* Prompt the user for a new Throttle position. This routine will
* loop until the user enters valid data, re-prompting the user each
* time.
*/
private static int readPosition(Scanner input) {
int position;
// Keep prompting until we get valid input.
while (true) {
System.out.print("Enter throttle position (0-6); enter -1 to quit: ");
try {
position = input.nextInt();
if (position >= -1 && position <= 6)
// Exit the loop if we have valid data.
break;
} catch (java.util.InputMismatchException e) {
/**
* We end up here if the user enters a non-integer response.
* This call discards the invalid token so that we can try
* reading new input.
*/
input.next();
}
/**
* If we get here, the user entered invalid data. Either they entered
* a number that it out of range, or they entered some non-numeric
* response. In either case, display an error and re-prompt.
*/
System.out.println("Invalid input.");
}
return position;
}
public static void main(String[] args) {
Throttle throttle;
Scanner input = new Scanner(System.in);
/**
* This is a try-with-resources [1] statement; it ensures that we properly
* close the scanner when we exit the try block. That's not strictly necessary
* in this code, but it stops my IDE from complaining about the unclosed scanner.
*
* [1]: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
*/
try (input) {
while(true) {
int position = readPosition(input);
if (position == -1) {
// Exit if user enters -1.
break;
} else {
// Otherwise create a new Throttle with updated position.
throttle = new Throttle(position);
}
System.out.println("Flow is now: " + throttle.flow());
}
}
}
}