【问题标题】:How to loop until user's input satisfies a condition?如何循环直到用户的输入满足条件?
【发布时间】:2015-06-27 21:41:05
【问题描述】:

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

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

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

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

        }
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你需要在条件上“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();
          }
      
      ...
      }
      

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

      【讨论】:

        【解决方案3】:

        只需将检查替换为 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);
        

        【讨论】:

        • 我认为您需要if(!firstRun),因为您在第一次运行时会打印错误消息。
        • 完全正确。您也可以用while 循环替换它,在进入循环之前第一次读取值并在进入whileloop 后立即打印错误消息
        • 现在怎么样了,这只会打印一次错误信息,这是你想要的吗?如果用户多次输入无效选项怎么办?
        • 哎呀,对不起,我犯了一个小错误if(firstRun)实际上应该是if(!firstRun)
        • 我很高兴我们今天终于可以达成一致 =D
        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-12-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-01-13
            • 2020-08-21
            • 2016-05-02
            • 2016-04-12
            相关资源
            最近更新 更多