英文:
Java 2d changing values for user input
问题
这段代码生成一个3行5列的二维数组。我需要获取用户输入并将所有小于用户输入值的生成值替换为100。运行这段代码可以完成我需要的所有操作,但我无法弄清楚如何获取用户输入,将其与随机值进行比较,并确定是否小于以将其更改为100。
import java.util.*;
public class secondArray {
public static void main(String[] args) {
int[][] array2 = new int[3][5];
Random rand = new Random();
Scanner input = new Scanner(System.in);
for(int r = 0; r < array2.length; r++) {
for( int c = 0; c < array2[r].length; c++) {
array2[r][c] = rand.nextInt(50-20 +1)+20;
}
}
System.out.println("The first array is: ");
for(int r = 0; r < array2.length; r++) {
for( int c = 0; c < array2[r].length; c++) {
System.out.print(array2[r][c] + " ");
}
System.out.println();
}
System.out.println("\nEnter a number for x: ");
int x;
x = input.nextInt();
System.out.println("\nArray after changing numbers less than "+ x +" :");
for(int r = 0; r < array2.length; r++) {
for( int c = 0; c < array2[r].length; c++) {
if (array2[r][c] < x) {
array2[r][c] = 100;
}
System.out.print(array2[r][c] + " ");
}
System.out.println();
}
}
}
英文:
This code generates a 3 X 5 2d array. I need to get the users input and replace all the generated values that are less than the users input to 100. Running this code does everything I need but I can't figure out how to get the users input to compare itself to the random values and determine if it is less to change it to 100.
<br>
import java.util.*;
public class secondArray {
public static void main(String[] args) {
int[][] array2 = new int[3][5];
Random rand = new Random();
Scanner input = new Scanner(System.in);
for(int r = 0; r < array2.length; r++) {
for( int c = 0; c < array2[r].length; c++) {
array2[r][c] = rand.nextInt(50-20 +1)+20;
}
}
System.out.println("The first array is: ");
for(int r = 0; r < array2.length; r++) {
for( int c = 0; c < array2[r].length; c++) {
System.out.print(array2[r][c] + " ");
}
System.out.println();
}
System.out.println("\nEnter a number for x: ");
int x;
x = input.nextInt();
System.out.println("\nArray after changing numbers less than "+ x +" :");
}
}
答案1
得分: 1
public class secondArray {
public static void main(String[] args) {
int[][] array2 = new int[3][5];
// 在函数顶部获取用户输入
x = input.nextInt();
Random rand = new Random();
Scanner input = new Scanner(System.in);
for(int r = 0; r < array2.length; r++) {
for( int c = 0; c < array2[r].length; c++) {
// 这里比较用户输入和随机数
int r = rand.nextInt(50-20 +1)+20;
if(r >= x)
array2[r][c] = r;
else:
array2[r][c] = 100;
}
}
System.out.println("第一个数组是: ");
for(int r = 0; r < array2.length; r++) {
for( int c = 0; c < array2[r].length; c++) {
System.out.print(array2[r][c] + " ");
}
System.out.println();
}
System.out.println("\n输入一个数字作为 x: ");
int x;
System.out.println("\n修改后的数组,小于 " + x + " 的数字:");
}
}
英文:
public class secondArray {
public static void main(String[] args) {
int[][] array2 = new int[3][5];
//make read user input in top of the function
x = input.nextInt();
Random rand = new Random();
Scanner input = new Scanner(System.in);
for(int r = 0; r < array2.length; r++) {
for( int c = 0; c < array2[r].length; c++) {
//Here where are comparing the input and the random number
int r = rand.nextInt(50-20 +1)+20;
if(r >= x)
array2[r][c] = r;
else:
array2[r][c] = 100;
}
}
System.out.println("The first array is: ");
for(int r = 0; r < array2.length; r++) {
for( int c = 0; c < array2[r].length; c++) {
System.out.print(array2[r][c] + " ");
}
System.out.println();
}
System.out.println("\nEnter a number for x: ");
int x;
System.out.println("\nArray after changing numbers less than "+ x +" :");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论