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

Java 在没有语法错误的情况下正确调用此方法时,为什么要跳过此方法?,java,methods,matrix,Java,Methods,Matrix,下面是正在发生的事情的基本代码。我已经提示输入行数和列数,这很有效。问题是,一旦程序到达“输入数据…”,它就会打印数据,但会完全忽略对矩阵的调用。在矩阵的内部我也会提示使用scanner类输入矩阵。而且,我意识到这一点目前还没有结束。请帮忙 case 1: System.out.println("Enter the data for the first matrix."); //input needed MatrixOne

下面是正在发生的事情的基本代码。我已经提示输入行数和列数,这很有效。问题是,一旦程序到达
“输入数据…”
,它就会打印数据,但会完全忽略对
矩阵的调用。在
矩阵的内部
我也会提示使用scanner类输入矩阵。而且,我意识到这一点目前还没有结束。请帮忙

 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;
}
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();
}
这是最重要的:

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

在矩阵方法中,如果行或列为零,则不会进入循环;因此,它们的值可能为零。

Java是

RowColumns
函数中,您将
通过值传递两个整数
,这意味着,只有它们的值传递给函数,而不是引用。因此,这些变量的实际引用不会受到影响。因此,在调用
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;
}

您的代码实际上调用的是
矩阵
方法,问题在于
行列
方法。您的
RowColumns
方法实际上并没有设置您认为是的变量,因此当调用
Matrix
方法时,传递给它的行和列变量为零

让我详细说明到底发生了什么。在java中,整数是通过值传递的,而不是通过引用传递的。这意味着当您调用
RowColumns(行,列)时真正发生的是行和列的实际值,并将其发送到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();
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();
}
这将完成同样的事情,但这次您将使用希望使用的
变量

如果希望将用户输入保留到自己的方法中,最直接的解决方案是创建两个单独的方法,一个用于获取行,另一个用于获取列,然后让这些方法返回用户给定的值。大概是这样的:

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();
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();
}
这两种方法看起来是这样的:

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

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

扫描仪的缓冲区中似乎仍有回车符/换行符。在Java中,尝试在每个
keyboard.nextLine
之后添加
keyboard.nextLine
,请对方法使用camalCase,对类使用PascalCase。您在哪里读取行和列的值?我不确定MadProgrammerRows和columns在一个单独的方法中,并在cases上面调用,这是什么意思,但输入2后,它们的值为2。对吗?试着在循环中打印i和j,因为我将初始化的行和列更改为2,它就运行了。我还是不明白为什么。你能解释一下吗?向我们展示整个main方法。行和列变量是全局变量吗?如果没有,它们将只在方法内部受到影响。将变量更改为CAMELCASE,这就是为什么查看所有代码非常重要的原因。