java - 我该如何使用方法?

标签 java arrays methods

我是java初学者。在我的程序中,我为随机 n 个数组、偶数索引、奇数元素、反转元素、数组的第一个、最后一个元素编写了代码。现在我想使用单独的方法来实现我的程序。我该怎么办
那?

public class Rand {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner (System.in);
             System.out.print("Enter the number of values:");

              int[] values = new int[input.nextInt()];
              Random rand = new Random();
              for (int i = 0; i < values.length; i++) {
                int n = rand.nextInt(20);
                values[i] = n;
              }
              System.out.print("Array: ");
              for (int i = 0; i < values.length; i++) {
                  System.out.print( " " + values[i]);
                }

              System.out.println();
              System.out.print("Even index: ");
              for (int i = 0; i < values.length; i++) {
                    if (i % 2 == 0) {
                        System.out.print( " " +values[i]);
                    }
              }
              System.out.println();
              System.out.print("Odd element:");
                for (int i = 0; i < values.length; i++) {
                    if (values[i] % 2 != 0) {
                        System.out.print(" " + values[i]);
                    }
                }
                System.out.println();
                System.out.print("Reverse order:");
                for (int i = values.length - 1; i > -1; i--) {
                    System.out.print( " "+ values[i]);
                }
                System.out.println();
                System.out.print("First, middle and last element:");
                System.out.print(" " + values[0]);

                if (values.length %2 ==0)
                {
                  System.out.print(" " + values[(values.length /2)-1]);
                  System.out.print(" " + values[values.length /2]);

                }
                System.out.print( " " + values[values.length - 1]);



        }

    }

最佳答案

举个例子,您可以在 main 中替换此代码

         Scanner input = new Scanner (System.in);
         System.out.print("Enter the number of values:");

          int[] values = new int[input.nextInt()];
          Random rand = new Random();
          for (int i = 0; i < values.length; i++) {
            int n = rand.nextInt(20);
            values[i] = n;
          }

         int[] values = getValues ();

其中 getValues 看起来像

private static int [] getValues () {
     Scanner input = new Scanner (System.in);
     System.out.print("Enter the number of values:");

      int[] values = new int[input.nextInt()];
      Random rand = new Random();
      for (int i = 0; i < values.length; i++) {
        int n = rand.nextInt(20);
        values[i] = n;
      }

      // lets also close the Scanner
      input.close ();
      return values;

关于java - 我该如何使用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40777131/

相关文章:

Java 记录和 Lombok 注释 - IntelliJ

c - 二维矩阵旋转与双线性插值

arrays - 我不明白为什么我的数组会产生我无法使用的输出

javascript - JavaScript中for循环访问数组时 'i'的作用是什么?

php - Laravel 私有(private)变量在 Controller 中的两个方法之间共享

c# - c# 中方法的菜单

java - 如何管理由于发布请求和历史记录而产生的 session 属性

java - 如何手动安装 java 库并将/tmp 保持为 noexec?

java - hibernate 异常 : Missing Column (column exists)

c++ - 原型(prototype)方法中的参数名称(头函数中的变量名称)