正在回答
3回答
您好,因为在主方法中将数组定义在了循环内部,当每次循环时数组都会再次为空,因此显示时数据都是0。修改:将定义数组的操作写在循环外部。
祝学习愉快!
披着鱼皮的猫
2018-12-06 13:45:36
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 | //定义第一个方法:插入数据 public int [] insertData() { Scanner sc = new Scanner(System.in); int [] arr = new int [ 10 ]; for ( int i= 0 ;i< 9 ;i++) { System.out.println( "请输入第" +(i+ 1 )+ "个数据" ); arr[i]=sc.nextInt(); arr[ 9 ]= 0 ; if (arr[i]== 0 ) { System.out.println( "输入的数据不能为0" ); i--; } } return arr; //返回值 } //定义第二个方法:显示所有数据 public void showData( int [] a, int length) { System.out.println( "显示所有的数据:" ); for ( int i = 0 ;i<a.length;i++) { System.out.print(a[i]+ " " ); } System.out.println(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); DateManage date = new DateManage(); date.notice(); while ( true ) { int n = sc.nextInt(); int a[] = new int [ 10 ]; switch (n) { case 1 : a=date.insertData(); date.notice(); break ; case 2 : date.showData(a, 10 ); date.notice(); break ; case 3 : int b = sc.nextInt(); int c = sc.nextInt(); date.insertAtArray(a, b, c);date.showData(a, 10 ); date.notice(); break ; case 4 : date.divThree(a);date.notice(); break ; case 0 : break ; default : System.out.println( "您数据的数据有误!" ); } } } |
我对主方法添加了a=date.insertDate();可是为什么之后还是输出的全为0呢,哪里出了问题呀?
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧