【问题标题】:Accepting 2 integers as console input & verify that each is an integer > 0接受 2 个整数作为控制台输入并验证每个整数是否 > 0
【发布时间】:2021-09-18 08:10:03
【问题描述】:

这是我的代码:

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

【问题讨论】:

    标签: java validation input integer


    【解决方案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 & " + 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 个提示捕获行和列。很抱歉造成混乱。
    • @RobertLewis 我已经发布了另一个答案,按照您的要求同时接受这两个答案。
    【解决方案2】:
    boolean b = (numRows > 0 && numColums > 0);
    System.out.println("Each number is > 0" + b);
    
    如果其中一个数字 b 将为假; 如果两者都 > 0,则 b 为真

    【讨论】:

      【解决方案3】:

      既然您希望输入是同时的,不妨试试:

      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.");
              
          }
      
      
          
      }
      

      【讨论】:

        猜你喜欢
        • 2011-06-15
        • 2017-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-20
        • 1970-01-01
        相关资源
        最近更新 更多