java – 如何从控制台接受用户数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何从控制台接受用户数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3160字,纯文字阅读大概需要5分钟。
内容图文
![java – 如何从控制台接受用户数据](/upload/InfoBanner/zyjiaocheng/745/04b8528c3b644927abc2f92786c0c6bf.jpg)
我的控制台java应用程序生成一个包含一些随机数的3×3矩阵.我想要做的是从集合中删除一些随机数,然后允许用户输入这些数字
到目前为止,我已经尝试了以下但是它不起作用
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();
}
}
}
}
编辑
我想要发生的是矩阵显示时缺少一些数字.然后用户将光标放在缺失的位置,然后输入数字
解决方法:
在您的代码中,您允许用户为显示的每个矩阵数选择多个数字,因此在打印一个数字之前需要多个输入.你想要这样的东西吗?
/* 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();
}
}
}
编辑:可能会发生相同的数字被此代码替换两次.
内容总结
以上是互联网集市为您收集整理的java – 如何从控制台接受用户数据全部内容,希望文章能够帮你解决java – 如何从控制台接受用户数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。