首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用方法?

如何使用方法?
EN

Stack Overflow用户
提问于 2016-11-24 10:23:22
回答 1查看 27关注 0票数 0

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

那?

代码语言:javascript
复制
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]);



        }

    }
EN

回答 1

Stack Overflow用户

发布于 2016-11-24 10:28:36

举个例子,你可以在你的main中替换这个代码

代码语言:javascript
复制
         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;
          }

使用

代码语言:javascript
复制
         int[] values = getValues ();

getValues看起来像什么

代码语言:javascript
复制
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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40777131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档