我写了一个小程序,将两个矩阵加在一起。每个矩阵必须具有相同数量的行和列。如果用户输入例如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");
}
}
}
答案 0 :(得分: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)
答案 1 :(得分: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
}
}
答案 2 :(得分:0)
不确定
while(!isValid(allInputs)) {
// Ask to enter inputs
}
// do something with valid inputs
...
private boolean isValid(MyTypes inputs) {
// are my inputs valid?
}
答案 3 :(得分: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");
}
}