java - 如何测试接收到的输入是否不是整数(不使用else语句)

标签 java testing input integer

我刚开始编程,我有一个问题。任何帮助将不胜感激。所以我想测试从扫描仪接收到的输入是否不是整数,如果不是整数我想 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/

相关文章:

testing - 如何衡量 End2End 测试覆盖率?

testing - PhpUnit 和 PhpRack 有什么区别?

excel - 用户定义的函数,它接受 Excel VBA 中的连续和不连续范围

Java:用户输入 - 扫描仪 - 第二次输入后程序挂起

javascript - 如何使用 nextAll 使用 jquery/查找所有输入

java - 在内部解释 split 和 contains 方法如何适用于 String

java - a4j :jsFunction actionListener not listening

java - Eclipse 使用运行时 exec 提供与 cmd 不同的输出

java - Spring AMQP RabbitMQ 代码中的发送方是否不提供交换类型?

ios - 如何知道 XCTestExpectation 当前履行计数