。。为什么插入数据后。。总显示0,并且选择插入元素的时候会被替换掉。。求帮忙看一下。。QAQ
来源:3-3 主方法的实现3及总结
Sircone
2019-04-23 02:55:16
package com.imooc.database; import java.util.InputMismatchException; import java.util.Scanner; public class DataBase { public int[] insertData() { int a[] = new int[10]; Scanner sc = new Scanner(System.in); for (int i = 0; i < a.length - 1; i++) { System.out.println("请输入" + (i + 1) + "个数据:"); try { a[i] = sc.nextInt(); } catch (java.util.InputMismatchException e) { System.out.println("输入的数据格式有错误,请重新输入"); sc.next(); i--; } } return a; } public void divThree(int a[]) { String str = ""; int count = 0; for (int n : a) { if (n % 3 == 0) { str = str + n + " "; count++; } } if (count == 0) { System.out.println("数组中没有能够被3整出的数"); } else { System.out.println("数组中能被3整出的数有:" + str); } } /** * * @param a * 数组 * @param n * 要插入的数据 * @param k * 要插入数据的位置 */ public void insertAtArray(int a[], int n, int k) { for (int i = a.length - 1; i < k; i--) { a[i] = a[i - 1]; } a[k] = n; } 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("***************************************"); } public void showData(int a[], int length) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } public static void main(String[] args) { DataBase dc = new DataBase(); Scanner sc = new Scanner(System.in); int input=0; int a[] = null; int n = 0, k = 0; while (true) { dc.notice(); System.out.println("----------------请输入数字进行操作----------------"); try{ input = sc.nextInt(); }catch(InputMismatchException e){ System.out.println("请输入列表中的数字!"); sc.next(); continue; } if (input == 0) { System.out.println("程序退出!"); break; } switch (input) { case 1: a = dc.insertData();// 插入数据 System.out.println("当前插入的数据元素为:"); dc.showData(a, a.length - 1);// 显示数据 continue; case 2: if (a != null) { System.out.println("数组元素为:"); if (a[a.length - 1] == 0) { dc.showData(a, a.length - 1); } else { dc.showData(a, a.length); } } else { System.out.println("还未定义数据元素,请重新原则"); } break; case 3: if (a != null) { System.out.println("请输入要插入的数据"); try { n = sc.nextInt(); System.out.println("请输入要插入数据的位置"); k = sc.nextInt(); } catch (InputMismatchException e) { System.out.println("输入数据有误,请重新输入"); sc.next(); break; } dc.insertAtArray(a, n, k); dc.showData(a, a.length); } else { System.out.println("还未插入数据"); } break; case 4: if(a!=null){ dc.divThree(a); }else{ System.out.println("还未在数组中插入数据,请重新选择操作"); } break; } } /* * DataBase dc = new DataBase(); int a[] = dc.insertData(); * System.out.println("显示当前输入的数据值为:"); // for (int n : a) // * System.out.print(n + " "); // 其中a代表的是数组,a.length代表的是数据中的数据长度(第几个数 据) * // dc.showData(a, a.length); // 显示出全部的数据值 dc.showData(a, a.length - * 1);// 显示出最后一个数据值 Scanner sc = new Scanner(System.in); * System.out.println("请输入要插入的数据"); int n = sc.nextInt(); * System.out.println("情输入要插入数据的位置"); int k = sc.nextInt(); * dc.insertAtArray(a, n, k); dc.showData(a, a.length); dc.divThree(a); */ } }
2回答
吃吃吃鱼的猫
2019-04-23
同学你好,在指定位置处插入数据应该把指定位置处的数据依次往后移动,然后再给该位置赋值。所以定义一个for循环后,循环变量i初识值为a.length-1,判断条件应该是是i>=k(k为用户要插入数据的位置); i值递减。如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
冷火凉烟
2019-04-23
显示为0的话,在调用 showData() 方法的时候将传入的 length 长度减1就行了
dc.showData(a, a.length - 1);
插入元素被替换是你的 for 循环有问题,如果 k 是被指定插入的位置,i 是数组的长度,那么 i < k 一定是 false 的,也就不会执行大括号中的内容,所以将 for 循环中的判断语句改为 i > k 就好了。
相似问题