接受2个整数作为控制台输入&验证每个整数都是大于0的整数

uajslkp6  于 2021-08-25  发布在  Java
关注(0)|答案(3)|浏览(460)

这是我的密码:

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();
v440hwme

v440hwme1#

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 & " + 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;
    }

}

我希望以上内容能有所帮助。如果你需要帮助理解代码,我很乐意解释。

ee7vknir

ee7vknir2#

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

如果其中一个数字<=0,则b为假;如果两者都大于0,则b为真

bybem2ql

bybem2ql3#

由于您希望输入同时进行,请尝试:

import java.util.Scanner;

public class ArrayRowColumnInputVerify2 {

    public static void main(String[] args) {

        int[] dimensions = accept_user_input("ROWS & COLUMNS");
        System.out.println("The rows and columns you entered are : " +dimensions[0]  + " rows & " + 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 && cols>0) {
                input_correct=true;
            } else {
                System.out.print("The number you entered :  rows:" + rows + " & 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 & COLUMNS of the array : ");
            Scanner sc = new Scanner(System.in);
            rows = sc.nextInt();
            cols = sc.nextInt();
            if (rows>0 && cols>0) {
                input_correct=true;
            } else {
                System.out.print("The number you entered :  rows:" + rows + " & cols:"+ cols + " must be greater than 0. ");
            }
        }

        System.out.println("The rows and columns you entered are : " +rows  + " rows & " + cols + " columns.");

    }

}

相关问题