如何循环直到用户输入满足条件?

How to loop until user's input satisfies a condition?

所以我正在编写一个程序,提示用户输入两个矩阵的维数,根据这些维数创建两个随机矩阵,然后将它们相乘。

我在第一部分遇到了一些问题。我希望程序不断询问用户尺寸,直到他们输入可以相乘的尺寸(第一个矩阵中的列数 = 第二个矩阵中的行数)。

但是如何将新输入的值重新分配给相同的变量?? 这是我目前拥有的:

public class Multiply {
        public static void main(String[] args) {

                Scanner myScanner;
                myScanner = new Scanner( System.in );

                System.out.println("Enter the number of rows in Matrix #1: ");
                System.out.println("Enter the number of columns in Matrix #1: ");
                System.out.println("Enter the number of rows in Matrix #2: ");
                System.out.println("Enter the number of columns in Matrix #2: ");

                int mOne = myScanner.nextInt();
                int nOne = myScanner.nextInt();
                int mTwo = myScanner.nextInt();
                int nTwo = myScanner.nextInt();

                if(mOne<=0 || mTwo<=0 || nOne<=0 || nTwo<=0){
                        System.out.println("Sorry, these are not valid dimensional inpupts");
                        System.exit(0);
                }

                while (nOne!=mTwo){
                        System.out.println("Sorry these matrices cannot be multiplied, please enter new dimensions");
                }

        }
}

只需用 do-while 循环替换检查即可。

boolean firstRun = true;
do{
     if(firstRun)
           //print some errormessage here
     else 
          firstRun = false;
     //read the values here
}while(mOne<=0 || mTwo<=0 || nOne<=0 || nTwo<=0);

您需要"while"条件:

public class Multiply {  
    public static void main(String[] args) { 

    Scanner myScanner;
    myScanner = new Scanner( System.in );

    System.out.println("Enter the number of rows in Matrix #1: ");
    System.out.println("Enter the number of columns in Matrix #1: ");
    System.out.println("Enter the number of rows in Matrix #2: ");
    System.out.println("Enter the number of columns in Matrix #2: ");

    int mOne = myScanner.nextInt();
    int nOne = myScanner.nextInt();
    int mTwo = myScanner.nextInt();
    int nTwo = myScanner.nextInt();

    while(mOne<=0 || mTwo<=0 || nOne<=0 || nTwo<=0){
        System.out.println("Sorry, these are not valid dimensional inpupts");
        mOne = myScanner.nextInt();
        nOne = myScanner.nextInt();
        mTwo = myScanner.nextInt();
        nTwo = myScanner.nextInt();
    }

...
}

这与您需要对输入数据应用的其他条件相同。

收集输入时执行以下操作:

int mOne = 0;
do{
  mOne = myScanner.nextInt();
}while(/*mOne is what you want*/);

这将循环直到给出的答案是你想要的。您也可以这样做来 post 一个错误:

int mOne = 0;
    do{
      mOne = myScanner.nextInt();
      if(/*mOne is not what you want*/){
         //Tell user
      }else{
        break;
      }
    }while(true);

不要将while循环放在最后,而是将整个代码挂在while循环中:

       do{        // Put the while loop here
            Scanner myScanner;
            myScanner = new Scanner( System.in );

            System.out.println("Enter the number of rows in Matrix #1: ");
            int mOne = myScanner.nextInt();    // Also, I suggest doing this to have
                                               // to have the input immediately after the
                                               // print statement.
            System.out.println("Enter the number of columns in Matrix #1: ");
            int nOne = myScanner.nextInt();
            System.out.println("Enter the number of rows in Matrix #2: ");
            int mTwo = myScanner.nextInt();
            System.out.println("Enter the number of columns in Matrix #2: ");
            int nTwo = myScanner.nextInt();






            if(mOne<=0 || mTwo<=0 || nOne<=0 || nTwo<=0){
                    System.out.println("Sorry, these are not valid dimensional inpupts");
                    System.exit(0);
            }


                    System.out.println("Sorry these matrices cannot be multiplied, please enter new dimensions");
        }while (nOne!=mTwo);

您可以使用 do...while 循环

 public class Multiply {
    public static void main(String[] args) {

            Scanner myScanner;
            myScanner = new Scanner( System.in );

            int mOne =0;
            int nOne = 0;
            int mTwo = 0;
            int nTwo = 0; 

            do {
                System.out.println("Enter the number of rows in Matrix #1: ");
                System.out.println("Enter the number of columns in Matrix #1: ");
                System.out.println("Enter the number of rows in Matrix #2: ");
                System.out.println("Enter the number of columns in Matrix  #2: ");

                mOne = myScanner.nextInt();
                nOne = myScanner.nextInt();
                mTwo = myScanner.nextInt();
                nTwo = myScanner.nextInt();

                if(mOne<=0 || mTwo<=0 || nOne<=0 || nTwo<=0){
                    System.out.println("Sorry, these are not valid     dimensional inpupts");
                    System.exit(0);
                }

                if (nOne != mTwo) {
                      System.out.println("Sorry these matrices cannot be     multiplied, please enter new dimensions");
                }

               } while (nOne!=mTwo);



            }
}