Принимаю 2 целых числа в качестве консольного ввода и проверяю, что каждое из них является целым числом > 0

#java #validation #input #integer

Вопрос:

Вот мой код:

 System.out.print("Enter the number of ROWS and COLUMNS of the array : ");
Scanner sc = new Scanner(System.in);
int numRows = sc.nextInt();
int numColumns = sc.nextInt();
 

Ответ №1:

 import java.util.Scanner;

public class ArrayRowColumnInputVerify {

    public static void main(String[] args) {
        
        int rows = accept_user_input("ROWS");
        int cols = accept_user_input("COLUMNS");
        System.out.println("The rows and columns you entered are : "  rows    " rows amp; "   cols   " columns.");
        
    }

    public static int  accept_user_input(String dimension) {
        int input_dimension=0;
        boolean input_correct=false;
        while(!input_correct) {
            System.out.print("Please, enter the number of "   dimension   " of the array : ");
            Scanner sc = new Scanner(System.in);
            input_dimension = sc.nextInt();
            if (input_dimension>0) {
                input_correct=true;
            } else {
                System.out.print("The number you entered "   input_dimension   " must be greater than 0. ");
            }
        }
        return input_dimension;
    }
    
}
 

Я надеюсь, что вышесказанное поможет. Если вам нужна помощь в понимании кода, я буду рад объяснить.

Комментарии:

1. Требуется, чтобы строки и столбцы были записаны с помощью 1 приглашения. Извините за путаницу.

2. @RobertLewis Я опубликовал еще один ответ, принимающий оба одновременно, как вы просили.

Ответ №2:

 boolean b = (numRows > 0 amp;amp; numColums > 0);
System.out.println("Each number is > 0"   b);
 

b будет ложным, если одно из чисел;
b будет истинным, если оба > 0

Ответ №3:

Поскольку вы хотите, чтобы ввод был одновременным, возможно, попробуйте :

 import java.util.Scanner;

public class ArrayRowColumnInputVerify2 {

    public static void main(String[] args) {
        
        int[] dimensions = accept_user_input("ROWS amp; COLUMNS");
        System.out.println("The rows and columns you entered are : "  dimensions[0]    " rows amp; "   dimensions[1]   " columns.");
        
    }

    public static int[]  accept_user_input(String dimension) {
        int rows=0;
        int cols=0;

        boolean input_correct=false;
        while(!input_correct) {
            System.out.print("Please, enter the number of "   dimension   " of the array : ");
            Scanner sc = new Scanner(System.in);
            rows = sc.nextInt();
            cols = sc.nextInt();
            if (rows>0 amp;amp; cols>0) {
                input_correct=true;
            } else {
                System.out.print("The number you entered :  rows:"   rows   " amp; cols:"  cols   " must be greater than 0. ");
            }
        }
        int[] input = {rows, cols};
        return input;
    }
    
}
 

Дальнейшее упрощение:

 import java.util.Scanner;

public class ArrayRowColumnInputVerify2 {

    public static void main(String[] args) {

        int rows=0;
        int cols=0;

        boolean input_correct=false;
        while(!input_correct) {
        System.out.print("Please, enter the number of ROWS amp; COLUMNS of the array : ");
            Scanner sc = new Scanner(System.in);
            rows = sc.nextInt();
            cols = sc.nextInt();
            if (rows>0 amp;amp; cols>0) {
                input_correct=true;
            } else {
                System.out.print("The number you entered :  rows:"   rows   " amp; cols:"  cols   " must be greater than 0. ");
            }
        }
        
        System.out.println("The rows and columns you entered are : "  rows    " rows amp; "   cols   " columns.");
        
    }


    
}