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

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

这是我的密码:

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

v440hwme1#

  1. import java.util.Scanner;
  2. public class ArrayRowColumnInputVerify {
  3. public static void main(String[] args) {
  4. int rows = accept_user_input("ROWS");
  5. int cols = accept_user_input("COLUMNS");
  6. System.out.println("The rows and columns you entered are : " +rows + " rows & " + cols + " columns.");
  7. }
  8. public static int accept_user_input(String dimension) {
  9. int input_dimension=0;
  10. boolean input_correct=false;
  11. while(!input_correct) {
  12. System.out.print("Please, enter the number of " + dimension + " of the array : ");
  13. Scanner sc = new Scanner(System.in);
  14. input_dimension = sc.nextInt();
  15. if (input_dimension>0) {
  16. input_correct=true;
  17. } else {
  18. System.out.print("The number you entered " + input_dimension + " must be greater than 0. ");
  19. }
  20. }
  21. return input_dimension;
  22. }
  23. }

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

展开查看全部
ee7vknir

ee7vknir2#

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

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

bybem2ql

bybem2ql3#

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

  1. import java.util.Scanner;
  2. public class ArrayRowColumnInputVerify2 {
  3. public static void main(String[] args) {
  4. int[] dimensions = accept_user_input("ROWS & COLUMNS");
  5. System.out.println("The rows and columns you entered are : " +dimensions[0] + " rows & " + dimensions[1] + " columns.");
  6. }
  7. public static int[] accept_user_input(String dimension) {
  8. int rows=0;
  9. int cols=0;
  10. boolean input_correct=false;
  11. while(!input_correct) {
  12. System.out.print("Please, enter the number of " + dimension + " of the array : ");
  13. Scanner sc = new Scanner(System.in);
  14. rows = sc.nextInt();
  15. cols = sc.nextInt();
  16. if (rows>0 && cols>0) {
  17. input_correct=true;
  18. } else {
  19. System.out.print("The number you entered : rows:" + rows + " & cols:"+ cols + " must be greater than 0. ");
  20. }
  21. }
  22. int[] input = {rows, cols};
  23. return input;
  24. }
  25. }

进一步简化:

  1. import java.util.Scanner;
  2. public class ArrayRowColumnInputVerify2 {
  3. public static void main(String[] args) {
  4. int rows=0;
  5. int cols=0;
  6. boolean input_correct=false;
  7. while(!input_correct) {
  8. System.out.print("Please, enter the number of ROWS & COLUMNS of the array : ");
  9. Scanner sc = new Scanner(System.in);
  10. rows = sc.nextInt();
  11. cols = sc.nextInt();
  12. if (rows>0 && cols>0) {
  13. input_correct=true;
  14. } else {
  15. System.out.print("The number you entered : rows:" + rows + " & cols:"+ cols + " must be greater than 0. ");
  16. }
  17. }
  18. System.out.println("The rows and columns you entered are : " +rows + " rows & " + cols + " columns.");
  19. }
  20. }
展开查看全部

相关问题