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

时间:2015-04-20 23:22:56

标签: java loops matrix variable-assignment user-input

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

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

但是如何将新输入的值重新分配给相同的变量? 这是我到目前为止所做的:

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

        }
}

5 个答案:

答案 0 :(得分:2)

您可以使用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);



            }
}

答案 1 :(得分:1)

只需用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);

答案 2 :(得分:1)

你需要&#34;而#34;在条件:

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

...
}

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

答案 3 :(得分:0)

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

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

这将循环,直到给出的答案是你想要的。您也可以这样做以发布错误:

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

答案 4 :(得分:0)

不是将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);