gpt4 book ai didi

java - 如何验证以便用户仅输入整数

转载 作者:行者123 更新时间:2023-12-02 09:26:39 27 4
gpt4 key购买 nike

我一直在尝试让我的代码正常工作,以便用户只能输入整数。但是,当我输入带有“awsd”等字符的内容时,它不断崩溃。我尝试使用 bool 来提供帮助,但它只能捕获负输入。另外,输入法必须以整数开头,所以我无法将其切换为字符串。请帮忙。

import java.util.Scanner;   // Needed for Scanner class
import java.io.*; // Needed for File I/O classes

public class Reverse {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
String Continue = "yes";
int num;

//creates the file name
File fileWR = new File("outDataFile.txt");
//creates the file object
fileWR.createNewFile();
//file scanner
BufferedWriter output = new BufferedWriter(new FileWriter(fileWR, true));

while (Continue.equals("yes")) {
System.out.print("Enter an integer number greater than 0 :");
num = keyboard.nextInt();
keyboard.nextLine();
if (fileWR.exists())
{
validate(num, output);
}
else
{
fileWR.createNewFile();
}

//option if the user wants to continue
System.out.println("Do you wish to continue?(yes or no): ");
Continue = keyboard.nextLine();
}
output.close();
}


public static void validate(int num, BufferedWriter output) throws IOException {
Scanner keyboard = new Scanner(System.in);
while(!checkNum(num))
{
System.out.print("That is not an integer greater than 0, please try again: ");
num = keyboard.nextInt();
keyboard.nextLine();
}
System.out.print("The original numbers are " + num +"\n");
output.write("\r\nThe original numbers are " + num +"\r\n");
reverse (num, output);
even (num, output);
odd(num, output);

}// end of public static void validate

public static void reverse(int num, BufferedWriter output) throws IOException {
String input = String.valueOf(num); //must output result within the void method for it to count as a void method
String result = ""; //otherwise, you cannot output it in the main method.

for (int i = (input.length() - 1); i >= 0; i--)
{
result = result + input.charAt(i)+' ';
}
result = "the number reversed "+ result +"\r\n";
System.out.print(result);
output.write(result);
}// end of public static void reverse

public static void even(int num, BufferedWriter output) throws IOException {
String input = String.valueOf(num);
String result = "";

for (int i = 0; (i < input.length()); i++)
{
if (Character.getNumericValue(input.charAt(i)) % 2 == 0)
result = result + input.charAt(i) + ' ';
}
if (result == "") {
result = "There are no even digits" + "\r\n";
} else {
result = "the even digits are "+ result +"\r\n";
}
System.out.print(result);
output.write(result);
}// end of public static void even

public static void odd(int num, BufferedWriter output) throws IOException {
String input = String.valueOf(num);
String result = "";

for (int i = 0; (i < input.length()); i++)
{
if (Character.getNumericValue(input.charAt(i)) % 2 == 1)
{
result = result + input.charAt(i) + ' ';
}
}
if (result == "") {
result = "There are no odd digits" + "\r\n";
} else {
result = "the even odd digits are "+ result +"\r\n";
}
System.out.print(result);
output.write(result);
System.out.print("------------------------\n");
output.write("------------------------\n");

}// end of public static void odd
public static boolean checkNum(int num)
{
if(num > 0) {
return true;
}
else {
return false;
}

}
}

最佳答案

您应该首先获取字符串,然后尝试转换它:

try {
String numStr = keyboard.nextLine();
num = Integer.parseInt(numStr);
//Complete with your code
} catch (NumberFormatException ex) {
System.out.print("That is not an integer");
}

关于java - 如何验证以便用户仅输入整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58294989/

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