Java扫描程序限制整数
试图找出如何让我的程序限制输入小于1的整数,并限制在扫描仪中输入字符串。这是我的密码:Java扫描程序限制整数,java,java.util.scanner,Java,Java.util.scanner,试图找出如何让我的程序限制输入小于1的整数,并限制在扫描仪中输入字符串。这是我的密码: import java.util.Scanner; // Import scanner object import java.io.*; // Import for file and IOException public class Distance { public static void main(String[] args) throws IOException { int
import java.util.Scanner; // Import scanner object
import java.io.*; // Import for file and IOException
public class Distance {
public static void main(String[] args) throws IOException {
int distance;
int speed, time;
String filename;
System.out.println("Welcome to Distance Calculator.");
// Create a scanner keyboard for user input
Scanner keyboard = new Scanner(System.in);
// Vehicle speed
System.out.print("Vehicle speed (MPH): ");
speed = keyboard.nextInt();
while (!keyboard.hasNextInt()) {
System.out.print("Please enter a valid #: ");
speed = keyboard.nextInt();
if (speed < 1) {
System.out.print("Please enter a # greater then 1: ");
keyboard.nextInt();
}
}
System.out.print("Time vehicle traveled (HR): ");
while (!keyboard.hasNextInt()) {
time = keyboard.nextInt();
if (time < 1) {
System.out.print("Please enter a valid time: ");
speed = keyboard.nextInt();
}
}
time = keyboard.nextInt();
keyboard.nextLine(); // Consume next line
// Get filename
System.out.print("File name for saving: ");
filename = keyboard.nextLine();
// Open file
String filePath = "C:/Users/Nik/Desktop/";
PrintWriter outputFile = new PrintWriter(filePath + filename);
outputFile.println("Hour Distance Traveled");
outputFile.println("-----------------------------");
for (int hour = 1; hour <= time; hour++) {
distance = (speed * hour);
outputFile.println(hour + "\t\t\t" + (distance + " Mi"));
}
outputFile.close();
System.out.print("Date written to " + filePath + filename);
}
}
非常感谢您的帮助 我认为这应该行得通:
System.out.print("Vehicle speed (MPH): ");
speed = -1;
do {
System.out.println("Please enter a valid integer greater than 1");
if (keyboard.hasNextInt() {
speed = keyboard.nextInt();
}
} while (speed < 1)
我敢肯定问题是来自于!然而,在您的while循环中,我认为我已经成功地清理了代码
免责声明:我还没有测试过这段代码,看看它是否有效,不过我想我会尝试一下,希望它能有所帮助。我相信,像这样更改代码可能会像您希望的那样工作。插入非整数时,它会处理输入问题;插入非正整数时,它会处理正整数问题。我测试了它,我认为它是按预期的方式工作的。试试看
public static void main(String[] args) throws IOException{
int distance;
int speed, time;
String filename;
System.out.println("Welcome to Distance Calculator.");
// Create a scanner keyboard for user input
Scanner keyboard = new Scanner(System.in);
// Vehicle speed
System.out.print("Vehicle speed (MPH): ");
while (!keyboard.hasNextInt() ||
((speed = keyboard.nextInt()) < 1) ) {
System.out.print("Please enter a valid #: ");
keyboard.nextLine();
}
System.out.print("Time vehicle traveled (HR): ");
while (!keyboard.hasNextInt() ||
((time = keyboard.nextInt()) < 1) ) {
System.out.print("Please enter a valid #: ");
keyboard.nextLine();
}
keyboard.nextLine(); // Consume next line
// Get filename
System.out.print("File name for saving: ");
filename = keyboard.nextLine();
// Open file
String filePath = "C:/Users/Nik/Desktop/";
PrintWriter outputFile = new PrintWriter(filePath + filename);
outputFile.println("Hour Distance Traveled");
outputFile.println("-----------------------------");
for (int hour = 1; hour <= time; hour++) {
distance = (speed * hour);
outputFile.println(hour + "\t\t\t" + (distance + " Mi"));
}
outputFile.close();
System.out.print("Date written to " + filePath + filename);
}
我的最终代码:
int distance;
int speed = 0, time;
String filename;
boolean validInput = false; // Boolean for validating scanner input
System.out.println("Welcome to Distance Calculator.");
// Create a scanner keyboard for user input
Scanner keyboard = new Scanner(System.in);
// Vehicle speed
System.out.print("Vehicle speed (MPH): ");
// Method for validating user input
while (validInput == false) {
if (!keyboard.hasNextInt()) { // check if keyboard scanner !integer
System.out.print("Please enter a valid #: "); // prompts user for valid input
keyboard.nextLine(); // consumes next line
}
else {
speed = keyboard.nextInt();
if (speed < 1) { // validates if speed > 0
System.out.print("Please enter a value greater then 1: "); // prompts user for valid speed
keyboard.nextLine(); // consumes next line
}
else validInput = true; // if statements are passed then set bool to True and end loop
}
}
如果速度等于1怎么办?我更新了代码。它基本上是一个距离计算器,然后输出到一个文件。我只想允许速度大于1的数字输入0会发生什么?我只想允许输入大于0的整数非常感谢您的帮助!你的回应激发了我的最终结果。我最终创建了一个初始化为false的布尔值。