我刚开始编程,我有一个问题。任何帮助将不胜感激。所以我想测试从扫描仪接收到的输入是否不是整数,如果不是整数我想 System.out.println("The number you have entered is not valid"); 它用于密码生成器,如果他们没有输入他们想要的字符数的有效数字,它将返回说它无效。
不幸的是我不能使用else语句,因为有
这是我的代码:
import java.util.Random;
import java.util.Scanner;
public class pGenerator {
public static void main(String[] args) {
final String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //gives possible character selections
final int N = characters.length();
Random r = new Random(); //creates a new random function
Scanner input = new Scanner(System.in); //scanner enables user to input something
System.out.println("Enter number of characters you would like your password to be:"); //asks user the amount of characters they would like their password to be
int passwordcharacters = (int) input.nextDouble(); //
while (passwordcharacters >0) { //while the number of characters is more that 0, continue with following code
System.out.print(characters.charAt(r.nextInt(N))); //prints out a random character from the String characters
--passwordcharacters; //minuses 1 from number inputed and continues with code if it is more that 0
}
if (input.nextDouble() < 0) { //tests for if number is less than 0
System.out.println("The number you have entered is less than 0");
}
if (input.nextDouble() == 0); { //tests for if number is 0
System.out.println("The number you have entered is 0");
}
}
}
有没有办法做这样的事情(我知道这是错误的,但只是一个例子)
if (input.nextDouble() != integer); { //tests for if number is not an integer
System.out.println("The number you have entered is invalid");
感谢您的宝贵时间:)
编辑:// 大家好 :D 感谢您提供的所有帮助!它现在 100% 正确工作:)
代码如下:
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class pGenerator {
public static void main(String[] args) {
final String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //gives possible character selections
final int N = characters.length();
Random r = new Random(); //creates a new random function
Scanner input = new Scanner(System.in); //scanner enables user to input something
System.out.println("Enter number of characters you would like your password to be:"); //asks user the amount of characters they would like their password to be
int passwordcharacters = input.nextInt(); //
try{
if (passwordcharacters > 0){
while (passwordcharacters >0) { //while the number of characters is more that 0, continue with following code
System.out.print(characters.charAt(r.nextInt(N))); //prints out a random character from the String characters
--passwordcharacters; //minuses 1 from number inputed and continues with code if it is more that 0
}
}else{
System.out.println("The number you have entered is invalid.");
}
}
catch(InputMismatchException ex){
System.out.println("The character you have entered is invalid.");
}
}
}
最佳答案
使用try..catch()
:
while (true) {
System.out.println("Enter number of characters you would like your password to be:");
try{
int passwordcharacters=input.nextInt(); //Why do you use nextDouble?
} catch(NumberFormatException e) {
System.out.println("Invalid input");
input.nextLine();
continue;//loop again
}
break;
}
或者如@BackSlash 所建议的那样,您可以循环使用直到输入有效整数
while(!input.hasNextInt())
{
System.out.println("Invalid input!");
input.nextLine();
}
//Once the execution of the program reaches here,an integer was entered.So scan it
int passwordcharacters=input.nextInt();
关于java - 如何测试接收到的输入是否不是整数(不使用else语句),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27470229/