1

我写了一个小程序,将两个矩阵加在一起。每个矩阵必须具有相同的行数和列数。如果用户输入例如 2x2 + 3x3,是否有一种巧妙的方法可以让用户再次回到程序的开头?

   import java.util.*;
   import javax.swing.*;

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

        System.out.println("Please enter the number of rows you want in Matrix 1 => ");
        Scanner stdio = new Scanner (System.in);
        int rowA = stdio.nextInt();

        System.out.println("Please enter the number of columns you want in Matrix 1 => ");
        int columnA = stdio.nextInt();

        System.out.println("Please enter the number of rows you want in Matrix 2 => ");
        int rowB = stdio.nextInt();

        System.out.println("Please enter the number of columns you want in Matrix 2 => ");
        int columnB = stdio.nextInt();

        if (rowA != rowB || columnA != columnB)
        {
            System.out.println("Matrix 1 and Matrix 2 must have same number of rows and columns");
        }

        int arrayA[][] = new int[rowA][columnA];

            for(int i = 0; i < rowA; i++)
            {
                for(int j = 0; j < columnA; j++)
                {
                    System.out.println("Please enter element for row " + (i+1) + " column "+ (j+1) + " of Matrix 1 => ");
                    arrayA[i][j] = stdio.nextInt();
                }
            }

        int arrayB[][] = new int[rowB][columnB];

            for(int i = 0; i < rowB; i++)
            {
                for(int j = 0; j < columnB; j++)
                {
                    System.out.println("Please enter element for row " + (i+1) + " column "+ (j+1) + " of Matrix 2 => ");
                    arrayB[i][j] = stdio.nextInt();
                }
            }


        int arrayC[][] = new int[rowA][columnA];

        for(int i = 0; i < rowB; i++)
        {
            for(int j = 0; j < columnB; j++)
            {
                arrayC[i][j] = arrayA[i][j] + arrayB[i][j];
            }
        }


        for(int i = 0; i < rowA; i++)
        {
            System.out.print("[");

            for(int j = 0; j < columnA; j++)
            {
                System.out.print(" " + arrayC[i][j]);

            }

            System.out.print(" ] " + "\n");

        }   


    }

           }
4

4 回答 4

0

如果您想像计算器一样无限运行它,我建议您执行以下操作

     while(true) {
         System.out.println("Please enter the number of rows you want in Matrix 1 => ");
         Scanner stdio = new Scanner (System.in);
         int rowA = stdio.nextInt();

         System.out.println("Please enter the number of columns you want in Matrix 1 => ");
         int columnA = stdio.nextInt();

         System.out.println("Please enter the number of rows you want in Matrix 2 => ");
         int rowB = stdio.nextInt();

         System.out.println("Please enter the number of columns you want in Matrix 2 => ");
         int columnB = stdio.nextInt();

         int decide = (rowA+columnA) - (rowB+columnB);
         switch(decide) {
             case 0 : //your code logic;
                 break;
             default:
                 System.out.println("rows and cols of both matrices must be equal");
         }
     }
于 2013-08-21T11:55:42.103 回答
0

您可以使用一个do...while(CONDITION)循环,CONDITION两个矩阵具有相同的大小。您应该将几行代码转换为:

do
{
    System.out.println("Please enter the number of rows you want in Matrix 1 => ");
    Scanner stdio = new Scanner (System.in);
    int rowA = stdio.nextInt();

    System.out.println("Please enter the number of columns you want in Matrix 1 => ");
    int columnA = stdio.nextInt();

    System.out.println("Please enter the number of rows you want in Matrix 2 => ");
    int rowB = stdio.nextInt();

    System.out.println("Please enter the number of columns you want in Matrix 2 => ");
    int columnB = stdio.nextInt();

    if (rowA != rowB || columnA != columnB)
    {
        System.out.println("Matrix 1 and Matrix 2 must have same number of rows and columns");
    }
}while(rowA != rowB || columnA != columnB)
于 2013-08-21T11:51:24.243 回答
0

将所有逻辑放在另一个函数中,让我们将其命名为DoSomething返回布尔值并执行

while(!DoSomething());

DoSomething在两个不同大小的矩阵的情况下,返回false

完整代码:

   import java.util.*;
   import javax.swing.*;

   public class MatrixAdd {

   public static void main (String[] args)
   {
        while(DoSomething() == false){}
   }

   private static boolean DoSomething()
      {

        System.out.println("Please enter the number of rows you want in Matrix 1 => ");
        Scanner stdio = new Scanner (System.in);
        int rowA = stdio.nextInt();

        System.out.println("Please enter the number of columns you want in Matrix 1 => ");
        int columnA = stdio.nextInt();

        System.out.println("Please enter the number of rows you want in Matrix 2 => ");
        int rowB = stdio.nextInt();

        System.out.println("Please enter the number of columns you want in Matrix 2 => ");
        int columnB = stdio.nextInt();

        if (rowA != rowB || columnA != columnB)
        {
            System.out.println("Matrix 1 and Matrix 2 must have same number of rows and columns");
             return false; /// MY EDIT OF THE FUNCTION
        }

        int arrayA[][] = new int[rowA][columnA];

            for(int i = 0; i < rowA; i++)
            {
                for(int j = 0; j < columnA; j++)
                {
                    System.out.println("Please enter element for row " + (i+1) + " column "+ (j+1) + " of Matrix 1 => ");
                    arrayA[i][j] = stdio.nextInt();
                }
            }

        int arrayB[][] = new int[rowB][columnB];

            for(int i = 0; i < rowB; i++)
            {
                for(int j = 0; j < columnB; j++)
                {
                    System.out.println("Please enter element for row " + (i+1) + " column "+ (j+1) + " of Matrix 2 => ");
                    arrayB[i][j] = stdio.nextInt();
                }
            }


        int arrayC[][] = new int[rowA][columnA];

        for(int i = 0; i < rowB; i++)
        {
            for(int j = 0; j < columnB; j++)
            {
                arrayC[i][j] = arrayA[i][j] + arrayB[i][j];
            }
        }


        for(int i = 0; i < rowA; i++)
        {
            System.out.print("[");

            for(int j = 0; j < columnA; j++)
            {
                System.out.print(" " + arrayC[i][j]);

            }

            System.out.print(" ] " + "\n");

        }   

        return true; /// MY EDIT OF THE FUNCTION
    }

   }
于 2013-08-21T11:51:38.300 回答
0

当然:

while(!isValid(allInputs)) {
    // Ask to enter inputs
}
// do something with valid inputs

...
private boolean isValid(MyTypes inputs) {
    // are my inputs valid?
}
于 2013-08-21T11:52:34.910 回答