Java 2D更改用户输入的值

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

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 &lt; array2.length; r++) {
for( int c = 0; c &lt; array2[r].length; c++) {
array2[r][c] = rand.nextInt(50-20 +1)+20;
}
}
System.out.println(&quot;The first array is: &quot;);
for(int r = 0; r &lt; array2.length; r++) {
for( int c = 0; c &lt; array2[r].length; c++) {
System.out.print(array2[r][c] + &quot; &quot;);
}
System.out.println();
}
System.out.println(&quot;\nEnter a number for x: &quot;);
int x;
x = input.nextInt();
System.out.println(&quot;\nArray after changing numbers less than &quot;+ x +&quot; :&quot;);
}
}

答案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 &lt; array2.length; r++) {
for( int c = 0; c &lt; array2[r].length; c++) {
//Here where are comparing the input and the random number
int r = rand.nextInt(50-20 +1)+20;
if(r &gt;= x)
array2[r][c] = r;
else:
array2[r][c] = 100;
}
}
System.out.println(&quot;The first array is: &quot;);
for(int r = 0; r &lt; array2.length; r++) {
for( int c = 0; c &lt; array2[r].length; c++) {
System.out.print(array2[r][c] + &quot; &quot;);
}
System.out.println();
}
System.out.println(&quot;\nEnter a number for x: &quot;);
int x;
System.out.println(&quot;\nArray after changing numbers less than &quot;+ x +&quot; :&quot;);
}
}

huangapple
  • 本文由 发表于 2020年9月20日 06:07:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63973797.html
匿名

发表评论

匿名网友

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

确定