下面是正在发生的事情的基本代码。我已经提示输入行数和列数,这是可行的。问题是,一旦程序到达"Enter the data...",它就会打印它,但是完全忽略了对Matrix的调用。在Matrix内部,我还提示输入一个使用扫描器类的矩阵。同时,我也意识到这件事目前还没有结束。请帮帮忙。
case 1:
System.out.println("Enter the data for the first matrix.");
//input needed
MatrixOne = Matrix(Rows, Columns);
System.out.println("+");
System.out.println("Enter the data for the second matrix.");
MatrixTwo = Matrix(Rows, Columns);
int[][]Plus = Add(MatrixOne, MatrixTwo, Rows, Columns);
Print(Rows, Columns, Plus);
break;以下是一种方法:
public static int [][] Matrix(int Rows, int Columns)
{
int[][] NewMatrix = new int[Rows][Columns];
for(int i=0; i<Rows; i++)
{
for(int j=0; j<Columns; j++)
{
System.out.println("HIIIIIIII");
NewMatrix[i][j] = keyboard.nextInt ();
}
}
return NewMatrix;
}以下是当前的输出:
Please choose an operation: Addition(1) or Subtract(2)1
How many Rows would you like your matrix to have?
2
How many Columns would you like your matrix to have?
2
Enter the data for the first matrix.
+
Enter the data for the second matrix.
=
Please choose an operation: Addition(1) or Subtract(2)这是最重要的:
int Rows=0;
int Columns=0;
int [][] MatrixOne;
int [][] MatrixTwo;
do{
System.out.print("Please choose an operation: Addition(1) or Subtract(2)");
operation = keyboard.nextInt();
RowColumns(Rows, Columns);这就是方法:
public static void RowColumns(int Rows, int Columns)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
Rows = keyboard.nextInt();
System.out.println("How many Columns would you like your matrix to have?");
Columns = keyboard.nextInt();
}和main
public static void main(String[] args)
{
String End;
int operation;
int Rows = 0;
int Columns = 0;
int [][] MatrixOne;
int [][] MatrixTwo;
do{
System.out.print("Please choose an operation: Addition(1) or Subtract(2)");
operation = keyboard.nextInt();
RowColumns(Rows, Columns);
/////////////////READS INPUT AND DETERMINES TO ADD OR SUBTRACT//////////////////////////////////
switch(operation)//Reads the value of the variable Operation. If Operation equals 1, the program will add the matrices,...
//if Operation equals 2, the program will subtract the matrices, and if the Operation equals anything other than 1 or 2, the...
//user will be prompted to enter either 1 or 2 again.
{
case 1:
System.out.println("Enter the data for the first matrix.");
//input needed
MatrixOne = Matrix(Rows, Columns);
System.out.println("+");
System.out.println("Enter the data for the second matrix.");
MatrixTwo = Matrix(Rows, Columns);
int[][]Plus = Add(MatrixOne, MatrixTwo, Rows, Columns);
Print(Rows, Columns, Plus);
break;
case 2:
System.out.println("Enter the data for the first matrix.");
MatrixOne = Matrix(Rows, Columns);
System.out.println("-");
System.out.println("Enter the data for the second matrix.");
MatrixTwo = Matrix(Rows, Columns);
int[][]Minus = Subtract(MatrixOne,MatrixTwo, Rows, Columns);
Print(Rows, Columns, Minus);
break;
default: System.out.println("Please enter 1 to add the matrices or 2 to subtract them.");
}//End of Switch
}while(operation != 1 || operation != 2);
}//End of main发布于 2014-11-05 01:56:45
在矩阵方法中,如果行或列为零,它将不会进入循环;因此,它们的值可能为零。
发布于 2014-11-05 02:28:56
Java是pass by value。
在RowColumns函数中,您是passing两个整数by value,这意味着,只有它们的值被传递给函数,而不是引用。因此,这些变量的实际引用不会受到影响。因此,在RowColumns(Rows, Columns)调用之后,Rows和Columns变量将与它们的初始值(0,0)保持不变。
如果要更改函数中变量的值,则应返回它们。
为此,您可以为这两个值定义一个容器:
public class Size {
public int Rows;
public int Columns;
}然后你可以初始化并使用它;
public static void Main(String[] args) {
...
Size size = new Size();
...
size = RowColumns(size);
...
}
public Size RowColumns(Size size)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
size.Rows = keyboard.nextInt();
System.out.println("How many Columns would you like your matrix to have?");
size.Columns = keyboard.nextInt();
return size;
}发布于 2014-11-05 02:38:03
您的代码实际上正在完全正确地调用Matrix方法,问题在于您的RowColumns方法。您的RowColumns方法实际上并没有设置您认为是的变量,所以当您的Matrix方法被称为行和列时,传递给它的变量为零。
让我详细分析一下到底发生了什么。在java中,整数是通过值传递的,而不是通过引用传递的。这意味着当您调用RowColumns(Row, Columns);时,真正发生的是行和列的实际值,并将其发送到RowColumns方法,而不是变量本身。实际上,这正在发生RowColumns(0, 0);
尽管RowColumns方法中的参数具有相同的名称,但它们不是相同的变量。下面是查看代码的另一种方法。
int Rows1=0;
int Columns1=0;
RowColumns(Rows1, Columns1);和你的RowColumns方法
public static void RowColumns(int Rows2, int Columns2)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
Rows2 = keyboard.nextInt();
System.out.println("How many Columns would you like your matrix to have?");
Columns2 = keyboard.nextInt();
}当RowColumns被调用时,Rows2被设置为Rows1,而Columns2被设置为Columns1,请注意您的RowColumns实际上是如何影响Rows1和Columns1的。在您的代码中,您将RowColumns的参数命名为与传递给它的变量相同,这意味着尽管它们看起来是相同的,但实际上它们是完全不同的。
解决这一问题的一个解决方案是,如果您不需要RowColumns方法,只需在以下地方使用它:
int Rows=0;
int Columns=0;
int [][] MatrixOne;
int [][] MatrixTwo;
do{
System.out.print("Please choose an operation: Addition(1) or Subtract(2)");
operation = keyboard.nextInt();
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
Rows = keyboard.nextInt();
System.out.println("How many Columns would you like your matrix to have?");
Columns = keyboard.nextInt();这将完成相同的任务,但这一次您将使用您想要的Rows和Columns变量。
如果要将用户输入保留到自己的方法中,最直接的解决方案是创建两个单独的方法,一个用于获取行,另一个用于获取列,然后让这些方法返回用户给出的值。就像这样:
int Rows=0;
int Columns=0;
int [][] MatrixOne;
int [][] MatrixTwo;
do{
System.out.print("Please choose an operation: Addition(1) or Subtract(2)");
operation = keyboard.nextInt();
Rows = getRows();
Columns = getColumns();这两种方法看起来都是这样的:
public static int getRows()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Rows would you like your matrix to have?");
return keyboard.nextInt();
}
public static int getColumns()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many Columns would you like your matrix to have?");
return keyboard.nextInt();
}解决这个问题的方法有很多种,我只是给出了两个最直接的方法。希望这能充分回答你的问题!
https://stackoverflow.com/questions/26748373
复制相似问题