英文:
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("Welcome to Matrix Determinant Calculator!");
System.out.println("Would you like to calculate the determinant of a new matrix?");
System.out.println("1. Yes"+ " "+ "2. No");
System.out.print("Option: ");
int i, j;
int option = input.nextInt();
if(option == 1){
System.out.println("What is the dimension of the matrix?");
System.out.println("1. 2x2 "+ " "+"2. 3x3");
System.out.print("Input: ");
int choice = input.nextInt();
if(choice == 1){ //matrix 2x2
int [][] input2x2 = new int[2][2];
System.out.println("Great! Let's calculate this determinant then.");
System.out.println("Please insert all the values for your matrix. Direction: [row][column]: your value\n");
for(i = 1; i<= 2;i++){
for( j = 1; j <= 2; j++){
System.out.print("New matrix: ["+i +"]"+ "["+ j+"]: ");
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< 2;i++){
for( j = 0; j < 2; j++){
System.out.print("New matrix: ["+i +"]"+ "["+ j+"]: ");
input2x2[i][j]=input.nextInt();
}
}
System.out.println("The determinant is : "+determinant(input2x2);
Note that `i` and `j` should start from 0 so you don'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
import java.util.Scanner;
class Main{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int i,j;
System.out.println("Enter the size of matrix you want : ");
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("Position X:["+i+"] Y:["+j+"]");
mat[i][j] = input.nextInt();
}
}
for (i = 0 ; i < x ; i++){
for (j = 0 ; j < x ; j++){
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
}
}
>EDIT : Why do only 2x2 and 3x3 matrix why limiting
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论