用while重复提示输入第1个数据,不用while如何再次输入不同项
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 | package homework; import java.util.Scanner; public class DateManage { //插入数据 public int [] insertData( int arr[]) { for ( int i = 1 ; i < arr.length; i++) { System.out.println( "请输入第" +i+ "个数据:" ); Scanner sc = new Scanner(System.in); arr[i- 1 ]=sc.nextInt(); } return arr; } //显示所有数据//显示几个数据 public void showData( int arr[], int length) { for ( int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+ " " ); } System.out.println(); } // //在指定位置处插入数据//要插入的数据插入数据的位置 // public void insertAtArray(int[] a,int n,int k) { // } // // //查询能被3整除的数据 // public void divThree(int[] a) { // } //显示提示信息 public void notice() { 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 static void main(String[] args) { int arr[] = new int [ 10 ]; DateManage d = new DateManage(); d.notice(); Scanner sc = new Scanner(System.in); Integer k = sc.nextInt(); while (k!= 0 ) { switch (k) { case 1 : d.insertData(arr); d.showData(arr,arr.length); d.notice(); break ; } } } } |
1
收起
正在回答
3回答
你好同学,如果不用while循环可以按照如下代码进行修改,在while循环外将k的值赋值为-1,这样就能进入while循环,然后在while循环内接收用户从键盘录入的数据,这样就不会循环插入数据了,祝学习愉快~
chrismorgen
2019-03-13 18:34:00
你好同学,建议你不要在return语句后添加run方法,return之后的代码是不能被执行的,所以才会报错哦,另外我针对同学的代码进行了修改,同学可以看一下,如果有什么不明白的地方,可以和我讨论一下哦,祝学习愉快~
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 | package homework; import java.util.Scanner; public class DateManage { // 插入数据 public int [] insertData( int arr[]) { for ( int i = 1 ; i < arr.length; i++) { System.out.println( "请输入第" + i + "个数据:" ); Scanner sc = new Scanner(System.in); arr[i - 1 ] = sc.nextInt(); } return arr; } // 显示所有数据//显示几个数据 public void showData( int arr[], int length) { for ( int i = 0 ; i < arr.length; i++) { System.out.print(arr[i] + " " ); } System.out.println(); run(); } // //在指定位置处插入数据//要插入的数据插入数据的位置 // public void insertAtArray(int[] a,int n,int k) { // } // // //查询能被3整除的数据 // public void divThree(int[] a) { // } // 显示提示信息 public void notice() { 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 static void main(String[] args) { run(); } static int arr[] = new int [ 10 ]; public static void run(){ DateManage d = new DateManage(); d.notice(); Scanner sc = new Scanner(System.in); Integer k = sc.nextInt(); switch (k) { case 1 : arr = d.insertData(arr); d.showData(arr, arr.length); d.notice(); break ; case 2 : d.showData(arr, arr.length); break ; } } } |
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧