为什么在没有语法错误的情况下正确调用此方法时会跳过此方法?

时间:2014-11-05 01:43:05

标签: java methods matrix

以下是正在进行的基本代码。我已经提示输入行数和列数,这是有效的。问题是一旦程序到达"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

3 个答案:

答案 0 :(得分:1)

Java是pass by value

RowColumns函数中,您是passing两个整数by value,这意味着只有它们的值传递给函数,而不是引用。因此,这些变量的实际引用不会受到影响。因此,在RowColumns(Rows, Columns)调用后,RowsColumns变量将与其初始值(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;
}

答案 1 :(得分:0)

在Matrix方法中,如果行或列为零,则不会进入循环;所以也许它们的值可能为零。

答案 2 :(得分:0)

您的代码实际上完全正在调用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被设置为Rows1Columns2被设置为等于Columns1时,请注意您的RowColumns如何进行操作#39;实际上影响Rows1Columns1。在您的代码中,您通过将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();

这可以完成同样的事情,但这次你将使用你想要的RowsColumns变量。

如果要将用户输入保持为自己的方法,最直接的解决方案是创建两个单独的方法,一个用于获取行,另一个用于获取列,然后让这些方法返回由用户。像这样:

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

有很多方法可以解决这个问题,我只给出了两个最直接的方法。希望这足以回答你的问题!