我在第一个方法里写好的数组怎么在第二个数组里调用啊

我在第一个方法里写好的数组怎么在第二个数组里调用啊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public int[] insertData()//插入数据
    {
        int[] a=new int[5];
        for(int i=0;i<a.length-1;i++){
          System.out.println("请输入第"+(i+1)+"个数据:");       
          Scanner input=new Scanner(System.in);
          a[i]=input.nextInt();
        }
        System.out.println("数据元素为:");
        for(int n=0;n<a.length;n++){
            System.out.print(a[n]+"    ");
        }
        notice();
        return a;
    }
    public void showData(int[] a,int length)//显示所有数据
    {
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+"    ");
        }
        notice();
    }


正在回答

登陆购买课程后可参与讨论,去登陆

4回答

我的意思是如下方式写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 public static void main(String[] args) {
         Test sc=new Test();
         int[] arr = new int[10];
         sc.notice();
         int n=0;
         do{
         Scanner input=new Scanner(System.in);
         n=input.nextInt();
         switch(n){
         case 1:
             arr = sc.insertData();
         case 2:
             sc.showData(arr, arr.length);
             }
         }while(n!=0);
    }


  • stajoaaa 提问者 #1
    非常感谢!
    2017-08-05 08:53:45
提问者 stajoaaa 2017-08-04 11:06:21

可是这样写的话,在第二个查询数组的方法执行时,又会跑一遍写数组的方法,又得重新写。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.imooc;
import java.util.Scanner;
public class Test {
    public void notice(){
        System.out.println();
        System.out.println("******************");
        System.out.println("1--插入数据");
        System.out.println("2--显示所有数据");
        System.out.println("3--在指定位置处插入数据");
        System.out.println("4--查询能被3整除的数据");
        System.out.println("0--退出");
        System.out.println("******************");
        System.out.println();
    }
    public int[] insertData()//插入数据
    {
        int[] a=new int[5];
        for(int i=0;i<a.length-1;i++){
          System.out.println("请输入第"+(i+1)+"个数据:");       
          Scanner input=new Scanner(System.in);
          a[i]=input.nextInt();
        }
        System.out.println("数据元素为:");
        for(int n=0;n<a.length;n++){
            System.out.print(a[n]+"    ");
        }
        notice();
        return a;
    }
    public void showData(int[] a,int length)//显示所有数据
    {
        System.out.println("数据元素为:");
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+"    ");
        }
        notice();
    }
    public void insertAtArray(int[] a,int n,int k)//在指定位置插入数据
    {
        System.out.println("请输入要插入的数据:");
        Scanner input1=new Scanner(System.in);
        n=input1.nextInt();
        System.out.println("请输入要插入的位置:");
        Scanner input2=new Scanner(System.in);
        k=input1.nextInt();
        a[k]=n;
        notice();
    }
    public void divTree(int[] a)//是否能被三整除
    {
        for(int i=0;i<a.length;i++)
        {
            if(a[i]%3==0){
                System.out.print(a[i]+"   ");
            }
        }
        notice();
    }
    public static void main(String[] args) {
         Test sc=new Test();
         sc.notice();
         int n=0;
         do{
         Scanner input=new Scanner(System.in);
         n=input.nextInt();
         switch(n){
         case 1:
             sc.insertData();
         case 2:
             int[] arr=sc.insertData();
             sc.showData(arr, arr.length);
             }
         }while(n!=0);
    }
 
}


Tender10 2017-08-04 11:01:25

1、现在我在main()方法中定义了一个数组arr;

2、case1语句下是不是要进行数据的插入也就是调用insertData()方法;

3、insertData()方法是有返回值的,返回的是一个数组,那么插入玩数据之后,是不是可以直接将这个方法的返回值赋值给arr。

Tender10 2017-08-04 10:39:05

其实你可以在main()方法开始的时候在定义一个数组,然后在switch1语句下插入完数据之后,将返回的数组赋值给main()方法中定义的数组,然后在case2语句中展示数据的时候,将这个数组传递进方法里即可。

  • 提问者 stajoaaa #1
    怎么把返回的数组赋值给main()方法中定义的数组啊
    2017-08-04 10:51:23
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码