Java 如何从控制台接受用户数据

Java 如何从控制台接受用户数据,java,console-application,bufferedreader,Java,Console Application,Bufferedreader,我的控制台java应用程序生成一个3x3矩阵,其中包含一些随机数。我要做的是从集合中删除一些随机数,并允许用户输入这些数字 到目前为止,我已经尝试了以下方法,但不起作用 package magicsquare; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; public class MagicSq

我的控制台java应用程序生成一个3x3矩阵,其中包含一些随机数。我要做的是从集合中删除一些随机数,并允许用户输入这些数字

到目前为止,我已经尝试了以下方法,但不起作用

   package magicsquare;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class MagicSquare {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\n\nEnter the size of the matrix : ");
        int n = Integer.parseInt(br.readLine());
        if (n > 5 && n < 2) {
            System.out.println("Enter a number between 2 to 5 ");
        } else {
            int A[][] = new int[n][n]; // Creating the Magic Matrix
            int i, j, k, t;
            /*Initializing every cell of the matrix with 0 */
            for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                    A[i][j] = 0;
                }
            }
            /* When the size of the matrix is Odd */
            if (n % 2 != 0) {
                i = 0;
                j = n / 2;
                k = 1;
                while (k <= n * n) {
                    A[i][j] = k++;
                    i--; // Making one step upward
                    j++; // Moving one step to the right 
                    if (i < 0 && j > n - 1) // Condition for the top-right corner element 
                    {
                        i = i + 2;
                        j--;
                    }
                    if (i < 0) // Wrapping around the row if it goes out of boundary 
                    {
                        i = n - 1;
                    }
                    if (j > n - 1) // Wrapping around the column if it goes out of boundary 
                    {
                        j = 0;
                    }
                    if (A[i][j] > 0) // Condition when the cell is already filled
                    {
                        i = i + 2;
                        j--;
                    }
                }
            } /* When the size of the matrix is even */ else {
                k = 1;
                /* Filling the matrix with natural numbers from 1 till n*n */
                for (i = 0; i < n; i++) {
                    for (j = 0; j < n; j++) {
                        A[i][j] = k++;
                    }
                }
                j = n - 1;
                for (i = 0; i < n / 2; i++) {
                    /* swapping corner elements of primary diagonal */
                    t = A[i][i];
                    A[i][i] = A[j][j];
                    A[j][j] = t;
                    /* swapping corner elements of secondary diagonal */
                    t = A[i][j];
                    A[i][j] = A[j][i];
                    A[j][i] = t;
                    j--;
                }
            }
            /* Printing the Magic matrix */
            System.out.println("The Magic Matrix of size " + n + "x" + n + " is:");
            for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                    //remove random element from array
                   Random rand = new Random();   
                  for (i = 0; i < 4 ; i++)
                  {
                  int randomNum1 = rand.nextInt((n-1) - 0 + 1) + 0;
                  int randomNum2 = rand.nextInt((n-1) - 0 + 1) + 0;
                  BufferedReader dr = new BufferedReader(new InputStreamReader(System.in));
                  A[randomNum1][randomNum2] = Integer.parseInt(dr.readLine());
                  }
                    System.out.print(A[i][j] + "\t");
                }
                System.out.println();

            }

        }
    }

}
package magicsquare;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.Random;
公共级魔法广场{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发IOException{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
System.out.print(“\n\n输入矩阵的大小:”);
int n=Integer.parseInt(br.readLine());
如果(n>5&&n<2){
System.out.println(“输入一个介于2到5之间的数字”);
}否则{
int A[][]=new int[n][n];//创建魔法矩阵
int i,j,k,t;
/*使用0初始化矩阵的每个单元格*/
对于(i=0;in-1)//如果列超出边界,则环绕该列
{
j=0;
}
if(A[i][j]>0)//单元格已填充时的条件
{
i=i+2;
j--;
}
}
}/*当矩阵的大小为偶数时*/else{
k=1;
/*用从1到n*n的自然数填充矩阵*/
对于(i=0;i
编辑

我想做的是,矩阵显示时缺少一些数字,然后用户将光标放在缺少的点上,然后输入数字

Scanner in = new Scanner(System.in);

int i = in.nextInt();
String s = in.next();

现在,您可以基于int i和String s的输入操作输出

在您的代码中,您允许用户为显示的矩阵的每个数字选择几个数字,因此在打印一个数字之前,它需要几个输入。你想要这样的吗

                /* Printing the Magic matrix */
        System.out.println("The Magic Matrix of size " + n + "x" + n + " is:");
        Random rand = new Random();
        for (i = 0; i < 2; i++) { //lets a user choose 2 numbers to be replaced
            System.out.println("Please input a number: ");
            int randomNum1 = rand.nextInt((n - 1) - 0 + 1) + 0;
            int randomNum2 = rand.nextInt((n - 1) - 0 + 1) + 0;
            BufferedReader dr = new BufferedReader(new InputStreamReader(System.in));
            A[randomNum1][randomNum2] = Integer.parseInt(dr.readLine());
        }

        for (i = 0; i < n; i++) {
            System.out.println("\n");
            for (j = 0; j < n; j++)
                System.out.print(A[i][j] + "\t");
        }
        System.out.println();

    }
}
/*打印魔法矩阵*/
System.out.println(“大小为“+n+”x“+n+”的魔法矩阵为:”);
Random rand=新的Random();
for(i=0;i<2;i++){//允许用户选择两个要替换的数字
System.out.println(“请输入一个数字:”);
int randomNum1=rand.nextInt((n-1)-0+1)+0;
int randomNum2=rand.nextInt((n-1)-0+1)+0;
BufferedReader dr=新的BufferedReader(新的InputStreamReader(System.in));
A[randomNum1][randomNum2]=Integer.parseInt(dr.readLine());
}
对于(i=0;i
}


编辑:同一个数字可能会被此代码替换两次。

仔细阅读了代码,除了打印和“渲染”矩阵的方式外,一切似乎都很好。因为在遍历表中每个值的循环中,对随机值使用5轮循环,所以随机值更改为矩阵大小的5倍。这与缓冲区读取的值在输入后保持不变这一事实相结合,使得数组的绘图变得凌乱,将输入的值作为单独的线保留在精细绘图循环中

            // render 
    drawMatrix(); //draw orginal values
    changeValues(); // change the random values
    drawMatrix(); // draw again to show the new values

}
            public void drawMatrix(){
        /* Printing the Magic matrix */
            System.out.println("The Magic Matrix of size " + n + "x" + n + " is:");
            for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                // print array current value
                System.out.print(A[i][j] + "\t");
            }
            System.out.println();
            }
        }

        public void changeValues(){
        //remove random element from array (5 times here, 0 to 4)
             Random rand = new Random();   
             for (i = 0; i < 4 ; i++)
             {
                int randomNum1 = rand.nextInt((n-1) - 0 + 1) + 0;
                int randomNum2 = rand.nextInt((n-1) - 0 + 1) + 0;
                BufferedReader dr = new BufferedReader(new InputStreamReader(System.in));
                A[randomNum1][randomNum2] = Integer.parseInt(dr.readLine());
             }
        }
//渲染
drawMatrix()//绘制原始值
changeValues();//更改随机值
drawMatrix();//再次绘制以显示新值
}
公共关系矩阵(){
/*打印魔法矩阵*/
System.out.println(“大小为“+n+”x“+n+”的魔法矩阵为:”);
对于(i=0