【问题标题】:create two dimensional array that can store integer values inside, and square them in different method and print them in another method创建可以在内部存储整数值的二维数组,并以不同的方法对它们进行平方并以另一种方法打印它们
【发布时间】:2021-06-21 20:40:35
【问题描述】:

完整的问题是:创建一个使用二维数组的程序,该数组可以在其中存储整数值。 (只需使用您自己定义的行和列创建一个数组)。创建一个名为 Square 的方法,该方法获取数组中的每个值并将其平方。制作另一个名为 ShowNumbers 的方法,它显示平方数。 我的尝试有错误:

public class SquareMatrix {
  public static void main(String[] args) {
  int sqr[][]= {{1,3,5},{2,4,6}};
  System.out.println("Your Original Matrix: ");
  for(int i = 0; i < 2; i++){
  for(int j = 0; j < 3; j++){
  System.out.print(sqr[i][j] + " ");
  }
  System.out.println();
  }
  Square();
  ShowNumbers();
  }
static void Square(){
    
  for(int i = 0; i <= 1; i++) {
  for(int j = 0; j <= 2; j++) {
  sqr[i][j] = sqr[i][j] * sqr[i][j];
  } 
  }
  }
  static void ShowNumbers(){
      
  System.out.println("Matrix after changes: ");
  for(int i = 0; i < 2; i++){
  for(int j = 0; j < 3; j++){
  System.out.print(sqr[i][j] + " ");   
  }
  System.out.println();
  }
} 
}

如果我想从用户那里获得 0 到 10 特定范围的 col 和 row 的输入,我将如何编写它,我制作了一个 替代版本,下面有很多错误。 下面的代码不如上面的重要

import java.util.Scanner;
public class MyClass {
    public static void main(String args[]) {
     
     int row, col, i, j;
       int arr[][] = new int[10][10];
       Scanner scan = new Scanner(System.in);
      try{
       System.out.print("Enter Number of Row for Array (max 10) : ");
       row = scan.nextInt();
       System.out.print("Enter Number of Column for Array (max 10) : ");
       col = scan.nextInt();
       if(0>=row && row<=10 || 0>=col && col<=10 ){
           }
      }
       catch (Exception e){
           System.out.println("(!) Wrong input...\n");
       }
       
       System.out.print("Enter " +(row*col)+ " Array Elements : ");
       for(i=0; i<row; i++){
           for(j=0; j<col; j++){
               arr[i][j] = scan.nextInt();
           }
       }
    }
       static void square(){
        arr[row][col] = arr[row][col] * arr[row][col];
       }
       
       static void ShowNumbers(){
       System.out.print("The Array is :\n");
       for(i=0; i<row; i++)
       {
           for(j=0; j<col; j++)
           {
               System.out.print(arr[i][j]+ "  ");
           }
           System.out.println();
       }}}

PS 我是 java 新手,希望能回复整个代码,而不仅仅是一段代码,这样我就不会迷失方向或错误所在的行号。时间> 感谢您的帮助

【问题讨论】:

  • 欢迎来到 Stack Overflow!请使用tour,环顾四周,并通读help center,尤其是I ask a good question?What topics can I ask about here? 是如何做到的。从第二个链接:“要求家庭作业帮助的问题必须包括您迄今为止为解决问题所做的工作的摘要,以及您在解决问题时遇到的困难的描述。”即使没有作业。请添加*完整的错误信息
  • 我投了反对票,因为"It's not working" is not helpful
  • 我不明白你在说什么。如果你真的想知道第一行代码有什么问题,Square 方法无法识别变量,第二个我已经在问题中提到了
  • @HussainAlAdraj 修复源代码的缩进。这使每个人(尤其是您)更容易阅读和理解代码。
  • @HussainAlAdraj 您可以将这些值作为参数提供给您的方法。这样你就可以调用这些方法,比如Square(sqr);

标签: java


【解决方案1】:

由于您是从零开始,最好再复习一下该语言的基础知识。看看oracle classvars 对于你上面的问题,你需要分别了解局部变量和实例变量的区别以及静态变量和非静态变量的区别。要解决您的问题,只需将数组的声明移出 main 方法并添加一个静态修饰符:

public class SquareMatrix {
    static int sqr[][] = {{1, 3, 5}, {2, 4, 6}};

    public static void main(String[] args) {

    //rest of your code
}

【讨论】:

  • @HussainAlAdraj 欢迎您。很高兴我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-30
  • 2022-11-29
  • 1970-01-01
  • 2017-03-25
  • 2013-06-04
  • 2020-04-13
相关资源
最近更新 更多