From f0df0318d186f2537108b75bba3c8f1b36238a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=96=E5=BF=83=E5=A6=8D?= <2392642810@qq.com> Date: Sun, 25 Dec 2022 11:42:54 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E4=BA=8C=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\263\225(\345\244\215\344\271\240).md" | 100 +++++---- ...41\347\220\206\347\263\273\347\273\237.md" | 211 ++++++++++++++++++ 2 files changed, 272 insertions(+), 39 deletions(-) create mode 100644 "03 \350\265\226\345\277\203\345\246\215/20221223 \344\271\246\347\261\215\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/03 \350\265\226\345\277\203\345\246\215/20221219 \347\254\254\344\272\214\346\254\241\346\226\271\346\263\225(\345\244\215\344\271\240).md" "b/03 \350\265\226\345\277\203\345\246\215/20221219 \347\254\254\344\272\214\346\254\241\346\226\271\346\263\225(\345\244\215\344\271\240).md" index d00823e..3b2b71f 100644 --- "a/03 \350\265\226\345\277\203\345\246\215/20221219 \347\254\254\344\272\214\346\254\241\346\226\271\346\263\225(\345\244\215\344\271\240).md" +++ "b/03 \350\265\226\345\277\203\345\246\215/20221219 \347\254\254\344\272\214\346\254\241\346\226\271\346\263\225(\345\244\215\344\271\240).md" @@ -157,20 +157,17 @@ import java.util.Scanner; public class D1 { public static void main(String[] args) { - Scanner sc = new Scanner(System.in); System.out.println("请输入圆的半径"); - double r = sc.nextDouble(); - - System.out.println("这个圆的周长是" + c(r)); - System.out.println("这个圆的面积是" + s(r)); - } - - public static double c(double r){ - return 2*3.14*r; + getX(new Scanner(System.in).nextDouble()); +// getX(new Scanner(System.in).nextDouble());的意思如下: +// Scanner sc = new Scanner(System.in); +// double r = sc.nextDouble(); +// getX(r); } - public static double s(double r){ - return 3.14*r*r; + public static void getX(double r){ + System.out.println("这个圆的周长是" +2*3.14*r); + System.out.println("这个圆的面积是" +3.14*r*r); } } ``` @@ -178,31 +175,38 @@ public class D1 { 在主函数中产生20个0-10之间的随机数,将这20个随机数存入数组,并通过函数计算某个数在这些随机数中出现的次数(这“某个数”是在主函数中由用户输入的) ```java +import java.util.Arrays; +import java.util.Random; +import java.util.Scanner; + public class D2 { public static void main(String[] args) { int [] num = new int[20]; Random random = new Random(); - for(int i=0;i<20;i++){ + for(int i=0;i num[i]) { min = num[i]; +// int y=i; } } @@ -252,42 +262,42 @@ public class D3 { } } - System.out.println(Arrays.toString(num)); + /* + int tmp=num[0]; + num[0]=num[x]; + num[x]=tmp; + + tmp=num[num.length-1]; + num[num.length-1]=num[y]; + num[y]=tmp + */ } } - - ``` 用自定义函数是实现求某数组元素的和(大小不固定) ```java +import javax.security.auth.login.CredentialNotFoundException; import java.util.Scanner; public class D4 { public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请问数组的长度是多少?"); - int [] num = new int[sc.nextInt()]; - - for(int i=0;i < num.length;i++){ - System.out.println("请输入第" + (i+1) + "个元素" ); - num[i]= sc.nextInt(); - } + int [] arr={11,22,33,44,55}; + int sum = getSum(arr); - arr(num); + System.out.println("数组和为" + sum); } - public static void arr(int[] num){ + public static int getSum(int[] arr){ int sum=0; - for(int i=0;i< num.length;i++){ - sum+=num[i]; + for(int i=0;i0){ + //退出系统 + break; + } + } + } + + //启动菜单,输出菜单 + public static void start(){ + System.out.println("" + + "==============================\n" + + "- 欢迎使用2班书籍管理系统 -\n" + + "-\t\t 1.浏览所有书籍信息\t-\n" + + "-\t\t 2.添加书籍信息\t\t-\n" + + "-\t\t 3.修改书籍信息\t\t-\n" + + "-\t\t 4.删除书籍信息\t\t-\n" + + "-\t\t 5.查询书籍信息\t\t-\n" + + "-\t\t 6.退出管理系统\t\t-\n" + + "=============================="); + } + + //选择菜单的功能,返回选择的值 + public static int choice(){ + System.out.println("请选择您的操作:"); + int num = sc.nextInt(); + return num; + } + + //根据用户输入的数字,匹配不同的功能 + public static int todo(int num) { + //方法接受到用户输入的num后,用switch判断分支 + int aa=0; + switch (num) { + case 1: + //1.浏览所有书籍信息 + viewAllBook(); + break; + case 2: + //2.添加书籍信息 + addBook(); + break; + case 3: + //3.修改书籍信息 + editBook(); + break; + case 4: + //4.删除书籍信息 + deleteBook(); + break; + case 5: + //5.查询书籍信息 + searchBook(); + break; + case 6: + //6.退出管理系统 + aa=1; + break; + default: + System.out.println("选择错误,没有该功能"); + break; + } + return aa; + } + + //查询书籍信息 + private static void searchBook() { + //用抽象来输入重复的代码,在input方法里 + String name = input("查询"); + //避免出现查询书籍信息不存在 + //循环代码重复,放在searchArr方法里面 + int index = searchArr(name); + if (index == -1){ + System.out.println("查不到此书"); + }else{ + System.out.println("这本书在" + (index+1) + "个"); + } + } + + //删除书籍信息m + private static void deleteBook() { + //用抽象来输入重复的代码,在input方法里 + String name = input("删除"); + //避免出现删除书籍信息不存在 + //循环代码重复,放在searchArr方法里面 + int index = searchArr(name); + if (index == -1){ + System.out.println("没有此书,无法删除"); + }else{ + book[index]=null; + System.out.println("删除成功"); + } + } + + //修改书籍信息 + private static void editBook() { + //用抽象来输入重复的代码,在input方法里 + String name = input("修改"); + //循环代码重复,放在searchArr方法里面 + int index = searchArr(name); + //修改名称 + if(index==-1){ + System.out.println("对不起,无法修改"); + }else{ + //用抽象来输入重复的代码,在input方法里 + String newName = input("新的");//newName存放这新的书籍名称 + book[index]=newName; + System.out.println("修改成功"); + } + } + + //添加书籍信息 + public static void addBook() { + //用抽象来输入重复的代码,在input方法里 + String name = input("添加"); + //避免出现二次添加 + //循环代码重复,放在searchArr方法里面 + int a = searchArr(name); + //-1代表着书籍数组里没有重复 + if (a==-1){ + int index = searchArr(null); + book[index] = name; + System.out.println("添加成功!"); + }else{ + System.out.println("已经存在" + name + ",请不要重复添加"); + } + } + + //显示所有书籍的方法 + private static void viewAllBook(){ + System.out.println("现在有以下书籍"); + //count是计数器,用来判断空值长度,若和数组一样,则没有书籍 + int count =0; + //现有以下书籍 + for (String name:book) { + //只需显示有值的书籍 + if(name==null){ + //统计次数 + count++; + //跳过这个if + continue; + } + System.out.println(name); + } + //判断有没有书籍 + if(count == book.length){ + System.out.println("一本书籍也没有"); + } + } + + //当出现很多重复代码时,可以创建一个方法,把代码抽出来,这个是循环代码 + public static int searchArr(String name){ + //因为需要数组索引 + int index = -1; + + //区分null和name,null不能用.equals比较 + if(name ==null){ + for (int i = 0; i < book.length; i++) { + if (book[i] == null) { + index=i; + break;//只需要第一个 + } + } + }else{ + for (int i = 0; i < book.length; i++) { + //字符串[]的比较要用.equals() + //null是空值,不能和其他的元素对比,但是别的元素可以和null对比 + if(name.equals(book[i])){ + index =i; + break;//只需要第一个 + } + } + } + return index; + } + + //重复的代码,这个是输入代码 + public static String input(String a) { + System.out.println("请输入你要" + a + "的书籍名称"); + String name = sc.next(); + return name; + } +} + + +``` + -- Gitee