我在第一个方法里写好的数组怎么在第二个数组里调用啊
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(); } |
源自:Java方法
8-1 综合案例需求
26
收起
正在回答
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
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 ); } } |
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧