用Java从扫描器输入打印矩阵的平方形状

huangapple go评论113阅读模式
英文:

Printing the matrix square shape from scanner input in java

问题

目前,我正在做一个关于矩阵行列式计算器的作业,我正在创建一个2x2的矩阵,已经完成了从用户那里获取值的部分,但是我不知道如何使用这些值进行更多的计算,以及如何打印出像这样的方形形状:

| 1 1|
| 1 1|

你们能帮我解决这个问题吗?这是我目前的代码:

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("欢迎使用矩阵行列式计算器!");
        System.out.println("您想要计算新矩阵的行列式吗?");
        System.out.println("1. 是" + "  " + "2. 否");
        System.out.print("选项: ");
        int i, j;
        int option = input.nextInt();
        if(option == 1){
            System.out.println("矩阵的维度是多少?");
            System.out.println("1. 2x2 " + "  " + "2. 3x3");
            System.out.print("输入: ");
            int choice = input.nextInt();
            if(choice == 1){ // 2x2矩阵
                int [][] input2x2 = new int[2][2];
                System.out.println("太好了!让我们来计算这个行列式。");
                System.out.println("请插入矩阵的所有值。方向:[行][列]:您的值\n");
                
                for(i = 0; i < 2; i++){
                    for(j = 0; j < 2; j++){
                        System.out.print("新矩阵: [" + i + "][" + j + "]: ");
                        int matrix2x2 = input.nextInt();
                    }
                }

注意:我只翻译了你提供的代码部分,其中所有的中英文标点符号都已经保留。如果你还有其他需要翻译的部分,可以继续提供。

英文:

currently, I'm doing an assignment about the matrix determinant calculator, I'm creating a matrix 2x2, I'm done with the taking value from the user part, but I don't know how can we use that value to do more calculation as well as printing the square shape like this:

| 1 1|
| 1 1|

Can you guys help me out with this, this is my code so far

import java.util.Scanner;
public class Main{
        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println(&quot;Welcome to Matrix Determinant Calculator!&quot;);
        System.out.println(&quot;Would you like to calculate the determinant of a new matrix?&quot;);
        System.out.println(&quot;1. Yes&quot;+ &quot;  &quot;+ &quot;2. No&quot;);
        System.out.print(&quot;Option: &quot;);
        int i, j;
        int option = input.nextInt();
        if(option == 1){
            System.out.println(&quot;What is the dimension of the matrix?&quot;);
            System.out.println(&quot;1. 2x2 &quot;+ &quot;  &quot;+&quot;2. 3x3&quot;);
            System.out.print(&quot;Input: &quot;);
            int choice = input.nextInt();
            if(choice == 1){ //matrix 2x2
                    int [][] input2x2 = new int[2][2];
                    System.out.println(&quot;Great! Let&#39;s calculate this determinant then.&quot;);
                    System.out.println(&quot;Please insert all the values for your matrix. Direction: [row][column]: your value\n&quot;);
                
                    for(i = 1; i&lt;= 2;i++){
                        for( j = 1; j &lt;= 2; j++){
                            System.out.print(&quot;New matrix: [&quot;+i +&quot;]&quot;+ &quot;[&quot;+ j+&quot;]: &quot;);
                            int matrix2x2 = input.nextInt();
                        }
                    }

答案1

得分: 0

你正在对2x2和3x3的索引进行硬编码。不要使用if语句。对2x2和3x3索引的打印应在单个嵌套的for循环中编码。

这个链接 是一个类似的问题,可能对您有帮助。

英文:

You're hard coding the 2x2 and the 3x3 indices. Don't use if statements. The printing of 2x2 and the 3x3 indices should be coded in a single nested for loop.

<a html="https://stackoverflow.com/questions/36939957/creating-a-matrix-in-java/36939999">this</a> is a similar question and should be of interest to you. https://stackoverflow.com/questions/36939957/creating-a-matrix-in-java/36939999

答案2

得分: 0

好的,以下是翻译好的内容:

计算2x2矩阵的行列式的公式,例如 {{a, b}, {c, d}},是 ad - bc。因此,在获取到矩阵后,你可以使用以下方法轻松计算:

public static int determinant(int[][] matrix2x2){
    return matrix2x2[0][0]*matrix2x2[1][1]  -
                  matrix2x2[0][1]*matrix2x2[1][0];
}

现在将此代码放置在 for 循环之后:

for(i = 0; i < 2; i++){
    for(j = 0; j < 2; j++){
        System.out.print("新矩阵: [" + i + "][" + j + "]: ");
        input2x2[i][j] = input.nextInt();
    }
}
System.out.println("行列式的值是: " + determinant(input2x2));
注意 `i``j` 应该从 0 开始这样就不会出现 `ArrayIndexOutOfBoundException`。

<details>
<summary>英文:</summary>

Well, the formula for calculating the determinant of a 2x2 matrix, like `{{a, b}, {c, d}}`, is `ad - bc`. So after getting the matrix you can easily calculate it with a method like the following:

    public static int determinant(int[][] matrix2x2){
        return matrix2x2[0][0]*matrix2x2[1][1]  -
                      matrix2x2[0][1]*matrix2x2[0][1];
    }
Now put this code after `for` loops:

    for(i = 0; i&lt; 2;i++){
        for( j = 0; j &lt; 2; j++){
            System.out.print(&quot;New matrix: [&quot;+i +&quot;]&quot;+ &quot;[&quot;+ j+&quot;]: &quot;);
            input2x2[i][j]=input.nextInt();
        }
    }
    System.out.println(&quot;The determinant is : &quot;+determinant(input2x2);
Note that `i` and `j` should start from 0 so you don&#39;t get a `ArrayIndexOutOfBoundException`.


</details>



# 答案3
**得分**: 0

```java
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int i, j;
        System.out.println("请输入你想要的矩阵大小:");
        System.out.print("X:");
        int x = input.nextInt();
        System.out.print("Y:");
        int y = input.nextInt();
        int[][] mat = new int[x][y];
        for (i = 0; i < x; i++) {
            for (j = 0; j < y; j++) {
                System.out.print("位置 X:[" + i + "] Y:[" + j + "]");
                mat[i][j] = input.nextInt();
            }
        }
        for (i = 0; i < x; i++) {
            for (j = 0; j < y; j++) {
                System.out.print(mat[i][j] + " ");
            }
            System.out.println();
        }
    }
}
编辑:为什么只有2x2和3x3的矩阵,为什么要限制呢 :)
英文:

> You Messed Up The Code Buddy So I Rewrote it 用Java从扫描器输入打印矩阵的平方形状

import java.util.Scanner;
class Main{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int i,j;
System.out.println(&quot;Enter the size of matrix you want : &quot;);
System.out.print(&quot;X : &quot;);
int x = input.nextInt();
System.out.print(&quot;Y : &quot;);
int y = input.nextInt();
int[][] mat=new int [x][y];
for (i = 0 ; i &lt; x ; i++){
for (j = 0 ; j &lt; y ; j++){
System.out.print(&quot;Position X:[&quot;+i+&quot;] Y:[&quot;+j+&quot;]&quot;);
mat[i][j] = input.nextInt();
}
}
for (i = 0 ; i &lt; x ; i++){
for (j = 0 ; j &lt; x ; j++){
System.out.print(mat[i][j]+&quot; &quot;);
}
System.out.println();
}
}
}

>EDIT : Why do only 2x2 and 3x3 matrix why limiting 用Java从扫描器输入打印矩阵的平方形状

huangapple
  • 本文由 发表于 2020年10月4日 15:01:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64191955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定