Java 2D更改用户输入的值

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

Java 2d changing values for user input

问题

这段代码生成一个3行5列的二维数组。我需要获取用户输入并将所有小于用户输入值的生成值替换为100。运行这段代码可以完成我需要的所有操作,但我无法弄清楚如何获取用户输入,将其与随机值进行比较,并确定是否小于以将其更改为100。

  1. import java.util.*;
  2. public class secondArray {
  3. public static void main(String[] args) {
  4. int[][] array2 = new int[3][5];
  5. Random rand = new Random();
  6. Scanner input = new Scanner(System.in);
  7. for(int r = 0; r < array2.length; r++) {
  8. for( int c = 0; c < array2[r].length; c++) {
  9. array2[r][c] = rand.nextInt(50-20 +1)+20;
  10. }
  11. }
  12. System.out.println("The first array is: ");
  13. for(int r = 0; r < array2.length; r++) {
  14. for( int c = 0; c < array2[r].length; c++) {
  15. System.out.print(array2[r][c] + " ");
  16. }
  17. System.out.println();
  18. }
  19. System.out.println("\nEnter a number for x: ");
  20. int x;
  21. x = input.nextInt();
  22. System.out.println("\nArray after changing numbers less than "+ x +" :");
  23. for(int r = 0; r < array2.length; r++) {
  24. for( int c = 0; c < array2[r].length; c++) {
  25. if (array2[r][c] < x) {
  26. array2[r][c] = 100;
  27. }
  28. System.out.print(array2[r][c] + " ");
  29. }
  30. System.out.println();
  31. }
  32. }
  33. }
英文:

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>

  1. import java.util.*;
  2. public class secondArray {
  3. public static void main(String[] args) {
  4. int[][] array2 = new int[3][5];
  5. Random rand = new Random();
  6. Scanner input = new Scanner(System.in);
  7. for(int r = 0; r &lt; array2.length; r++) {
  8. for( int c = 0; c &lt; array2[r].length; c++) {
  9. array2[r][c] = rand.nextInt(50-20 +1)+20;
  10. }
  11. }
  12. System.out.println(&quot;The first array is: &quot;);
  13. for(int r = 0; r &lt; array2.length; r++) {
  14. for( int c = 0; c &lt; array2[r].length; c++) {
  15. System.out.print(array2[r][c] + &quot; &quot;);
  16. }
  17. System.out.println();
  18. }
  19. System.out.println(&quot;\nEnter a number for x: &quot;);
  20. int x;
  21. x = input.nextInt();
  22. System.out.println(&quot;\nArray after changing numbers less than &quot;+ x +&quot; :&quot;);
  23. }
  24. }

答案1

得分: 1

  1. public class secondArray {
  2. public static void main(String[] args) {
  3. int[][] array2 = new int[3][5];
  4. // 在函数顶部获取用户输入
  5. x = input.nextInt();
  6. Random rand = new Random();
  7. Scanner input = new Scanner(System.in);
  8. for(int r = 0; r < array2.length; r++) {
  9. for( int c = 0; c < array2[r].length; c++) {
  10. // 这里比较用户输入和随机数
  11. int r = rand.nextInt(50-20 +1)+20;
  12. if(r >= x)
  13. array2[r][c] = r;
  14. else:
  15. array2[r][c] = 100;
  16. }
  17. }
  18. System.out.println("第一个数组是: ");
  19. for(int r = 0; r < array2.length; r++) {
  20. for( int c = 0; c < array2[r].length; c++) {
  21. System.out.print(array2[r][c] + " ");
  22. }
  23. System.out.println();
  24. }
  25. System.out.println("\n输入一个数字作为 x: ");
  26. int x;
  27. System.out.println("\n修改后的数组,小于 " + x + " 的数字:");
  28. }
  29. }
英文:
  1. public class secondArray {
  2. public static void main(String[] args) {
  3. int[][] array2 = new int[3][5];
  4. //make read user input in top of the function
  5. x = input.nextInt();
  6. Random rand = new Random();
  7. Scanner input = new Scanner(System.in);
  8. for(int r = 0; r &lt; array2.length; r++) {
  9. for( int c = 0; c &lt; array2[r].length; c++) {
  10. //Here where are comparing the input and the random number
  11. int r = rand.nextInt(50-20 +1)+20;
  12. if(r &gt;= x)
  13. array2[r][c] = r;
  14. else:
  15. array2[r][c] = 100;
  16. }
  17. }
  18. System.out.println(&quot;The first array is: &quot;);
  19. for(int r = 0; r &lt; array2.length; r++) {
  20. for( int c = 0; c &lt; array2[r].length; c++) {
  21. System.out.print(array2[r][c] + &quot; &quot;);
  22. }
  23. System.out.println();
  24. }
  25. System.out.println(&quot;\nEnter a number for x: &quot;);
  26. int x;
  27. System.out.println(&quot;\nArray after changing numbers less than &quot;+ x +&quot; :&quot;);
  28. }
  29. }

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:

确定