方法

 

 1 package com.unit4.test;
 2 
 3 public class Test1 {
 4     
 5     //返回修饰符public:项目中任何地方都可以调用
 6     //返回值类型void:无返回值
 7     //方法名function1:驼峰命名
 8     //()中写参数列表,可以省略
 9     //static静态方法:可以在本类中直接调用
10     public static void function1(){
11         //方法体
12         System.out.println("这是一个方法!");
13     }
14 
15     public static void main(String[] args) {
16         System.out.println("main方法被执行了!");
17         
18         //调用方法
19         function1();
20         
21     }
22 
23 }

 

 1 package com.unit4.test;
 2 
 3 public class Test2 {
 4     //参数列表和返回值
 5     
 6     //传入单个参数
 7     public static void function1(int num) {
 8         System.out.println("传参");
 9         System.out.println("数字是:"+num);
10     }
11     
12     //传入多个参数
13     public static void function2(String name,int num,String level) {
14         double total=num*(1+0.3);
15         System.out.println(level+"用户"+name+",您好,您的余额为:"+total);
16     }
17     
18     //传入不定长参数
19     public static void function3(int[] scores) {
20         int sum=0;
21         for(int x:scores) {
22             sum+=x;
23         }
24         double avg=sum*1.0/scores.length;
25         System.out.println(avg);
26     }
27     
28     //返回值
29     public static double function4(int[] scores) {
30         int sum=0;
31         for(int x:scores) {
32             sum+=x;
33         }
34         double avg=sum*1.0/scores.length;
35 //        System.out.println(avg);
36         return avg;  //将avg作为返回值传递到方法外面
37     }
38     
39     public static String function5(String fruit) {
40         System.out.println("榨汁机工作中");
41         String aa=fruit+"汁";
42         return aa;
43     }
44 
45     public static void main(String[] args) {
46         // TODO Auto-generated method stub
47         //function1(10);
48 //        function2("张三",10000,"钻石");
49         
50         int[] ss= {66,77,56,90,84};
51 //        function3(ss);
52         
53 //        double avg=function4(ss);
54 //        System.out.println(avg);
55         
56         String aa=function5("葡萄");
57         System.out.println(aa);
58 
59     }
60 
61 }

 

 1 package com.unit4.test;
 2 
 3 public class Test3 {
 4     public static void f1() {
 5         System.out.println("f1");
 6     }
 7     
 8     public static void f2() {
 9         System.out.println("f2");
10     }
11     
12     public static void f3() {
13         f1();
14         f2();
15     }
16     
17     //递归
18     public static int sum(int i,int result) {
19 //        System.out.println(i);
20         result+=i;
21         i++;
22         if(i<=100) {
23             result=sum(i,result);
24         }
25         return result;
26     }
27 
28     public static void main(String[] args) {
29 //        f3();
30         int re=sum(0,0);
31         System.out.println(re);
32 
33     }
34 
35 }

 

 1 package com.unit4.test;
 2 
 3 public class Test4 {
 4     // 练习一:体积计算器
 5     public static double getVol(double r,double h) {
 6         double vol=3.1415926*r*r*h;
 7         return vol;
 8     }
 9     
10     //练习二:修改密码
11     public static String changePwd(String oldpwd,String newpwd) {
12         String pwd="123456";
13         String msg="错误";
14         if(oldpwd.equals(pwd)) {
15             if(newpwd.length()<6) {
16                 System.out.println("请输入大于6位数的密码");
17             }else {
18                 pwd=newpwd;
19                 msg="正确";
20                 System.out.println("密码输入正确");
21             }
22         }else {
23             System.out.println("旧密码输入错误");
24         }
25         return msg;
26     }
27 
28     public static void main(String[] args) {
29 //        double vol=getVol(2, 5);
30 //        System.out.println(vol);
31         
32         String msg=changePwd("123456", "1239288");
33         System.out.println(msg);
34         
35 
36     }
37 
38 }

 

 1 package com.unit4.test;
 2 
 3 public class Test5 {
 4     // 变量作用域范围
 5     
 6     //定义在类中的变量作用范围是整个类
 7     static String a="定义在类中的变量";
 8             
 9     public static void f1() {
10         //定义在方法内部的变量,作用范围也只在方法内部,外部无法访问
11 //        String a1="定义在f1中的变量";
12 //        System.out.println(a1);
13         
14         String a="xxxx";
15         System.out.println("f1"+a);
16     }
17     
18     public static void f2() {
19         System.out.println("f2"+a);
20     }
21     
22     public static void f3() {
23         System.out.println("f3"+a);
24     }
25 
26     public static void main(String[] args) {
27         String a1="main变量";
28         System.out.println(a1);
29         
30         f1();
31         f2();
32         f3();
33         
34 
35     }
36 
37 }

 

  1 package com.unit4.test;
  2 
  3 import java.util.Scanner;
  4 
  5 public class Test6 {
  6     //例:迷你图书借阅系统
  7     static String[] nameArray= {"java教程","python入门","C语言教程","ps大神之路"};//书名
  8     static String[] authorArray= {"张三","李四","王五","赵柳"};//作者
  9     static int[] stateArray= {1,1,2,2};//状态:1.可借  2.不可借
 10     static int[] borrowCount= {67,70,43,15};//累计借出次数
 11     static Scanner s=new Scanner(System.in);
 12     
 13     //归还图书
 14     public static void returnBook() {
 15         System.out.println("请输入书名:");
 16         String bookname4=s.next();
 17         String msg="图书不存在!";
 18         double rent=0;
 19         for(int i=0;i<nameArray.length;i++) {
 20             if(bookname4.equals(nameArray[i])) {
 21                 if(stateArray[i]==2) {
 22                     System.out.println("请输入租借天数:");
 23                     int days=s.nextInt();
 24                     if(days>31 || days<1) {
 25                         System.out.println("输入错误!");
 26                         msg="归还失败!";
 27                     }else {
 28                         msg="图书已还!";
 29                         stateArray[i]=1;
 30                         rent=2.0*days;
 31                         System.out.println(bookname4+"归还成功!租金:"+rent+"元");
 32                     }
 33                 }else{
 34                     msg="输入错误,图书未被借出!";
 35                 }
 36             }
 37         }
 38         if("图书已还!".equals(msg)) {
 39             showBooks(nameArray,authorArray,stateArray,borrowCount);
 40         }else {
 41             System.out.println(msg);
 42         }
 43     }
 44     
 45     //借出图书
 46     public static void borrowBook() {
 47         System.out.println("请输入书名:");
 48         String bookname3=s.next();
 49         String msg="图书不存在!";
 50         for(int i=0;i<nameArray.length;i++) {
 51             if(bookname3.equals(nameArray[i])) {
 52                 if(stateArray[i]==2) {
 53                     msg="抱歉!图书已被借出!";
 54                 }else {
 55                     msg="图书可借!";
 56                     stateArray[i]=2;
 57                     borrowCount[i]+=1;
 58                 }
 59             }
 60         }
 61         if("图书可借!".equals(msg)) {
 62             System.out.println(bookname3+"借出成功!");
 63             showBooks(nameArray,authorArray,stateArray,borrowCount);
 64         }else {
 65             System.out.println(msg);
 66         }
 67         
 68     }
 69     
 70     //删除图书
 71     public static void delBook() {
 72         System.out.println("请输入书名:");
 73         String bookname1=s.next();
 74         int msg2=1;
 75         int index=0;
 76         for(int i=0;i<nameArray.length;i++) {
 77             if(bookname1.equals(nameArray[i])) {
 78                 msg2=2;
 79                 index=i;
 80             }
 81         }
 82         if(msg2==2) {
 83             String[] nameArray2=new String[nameArray.length-1];
 84             String[] authorArray2=new String[nameArray.length-1];
 85             int[] stateArray2=new int[nameArray.length-1];
 86             int[] borrowCount2=new int[nameArray.length-1];
 87             
 88             for(int i=0;i<nameArray2.length;i++) {
 89                 if(i<index) {
 90                     nameArray2[i]=nameArray[i];
 91                     authorArray2[i]=authorArray[i];
 92                     stateArray2[i]=stateArray[i];
 93                     borrowCount2[i]=borrowCount[i];
 94                 }else if(i>=index) {
 95                     nameArray2[i]=nameArray[i+1];
 96                     authorArray2[i]=authorArray[i+1];
 97                     stateArray2[i]=stateArray[i+1];
 98                     borrowCount2[i]=borrowCount[i]+1;
 99                 }
100             }
101             System.out.println("删除成功!添加书名:"+bookname1);
102             showBooks(nameArray2,authorArray2,stateArray2,borrowCount2);
103         }else {
104             System.out.println("图书不存在!");
105         }
106     }
107     
108     //增加图书
109     public static void addBook() {
110         if(nameArray.length>6) {
111             System.out.println("书架已满!");
112         }else {
113             System.out.println("请输入书名:");
114             String bookname=s.next();
115             System.out.println("请输入作者名:");
116             String author=s.next();
117             int msg1=1;
118             for(String n:nameArray) {
119                 if(bookname.equals(n)) {
120                     msg1=2;
121                 }
122             }
123             if(msg1==2) {
124                 System.out.println("该图书已存在!");
125             }else {
126                 String[] nameArray1=new String[nameArray.length+1];
127                 String[] authorArray1=new String[nameArray.length+1];
128                 int[] stateArray1=new int[nameArray.length+1];
129                 int[] borrowCount1=new int[nameArray.length+1];
130                 
131                 for(int i=0;i<nameArray.length;i++) {
132                     nameArray1[i]=nameArray[i];
133                     authorArray1[i]=authorArray[i];
134                     stateArray1[i]=stateArray[i];
135                     borrowCount1[i]=borrowCount[i];
136                 }
137                 nameArray1[nameArray1.length-1]=bookname;
138                 authorArray1[authorArray1.length-1]=author;
139                 stateArray1[stateArray1.length-1]=1;
140                 borrowCount1[borrowCount1.length-1]=0;
141                 
142                 System.out.println("添加成功!添加书名:"+bookname);
143                 showBooks(nameArray1,authorArray1,stateArray1,borrowCount1);
144             }
145         }
146             
147     }
148     
149     //展示图书
150     public static void showBooks(String a[],String b[],int c[],int d[]) {
151         System.out.println("书名---作者---状态---累计借出次数");
152         for(int i=0;i<a.length;i++) {
153             String status="可借";
154             if(c[i]==2) {
155                 status="不可借";
156             }
157             System.out.println(a[i]+"---"+b[i]+"---"+status+"---"+d[i]);
158         }
159     }
160     
161     //登录
162     public static String login() {
163         System.out.println("请输入用户名:");
164         String name=s.next();
165         System.out.println("请输入密码:");
166         String pwd=s.next();
167         String msg="错误";
168         
169         if("aaa".equals(name) && "123".equals(pwd)) {
170             msg="正确";
171         }
172         return msg;
173     }
174     
175     //显示主菜单
176     public static void showMenu() {
177         System.out.println("--------------主菜单---------------------");
178         System.out.println("1.查看所有图书");
179         System.out.println("2.增加图书");
180         System.out.println("3.删除图书");
181         System.out.println("4.借出图书");
182         System.out.println("5.归还图书");
183         System.out.println("6.退出");
184         System.out.println("请选择菜单编号:");
185     }
186 
187     public static void main(String[] args) {
188 
189         while(true) {
190             String msg=login();
191             if("错误".equals(msg)) {
192                 continue;
193             }else if("正确".equals(msg)) {
194                 while(true) {
195                     System.out.println("欢迎来到图书管理系统!");
196                     showMenu();
197                     int choice=s.nextInt();
198                     if(choice==1) {
199                         showBooks(nameArray,authorArray,stateArray,borrowCount);
200                     }else if(choice==2) {
201                         addBook();
202                     }else if(choice==3) {
203                         delBook();
204                     }else if(choice==4) {
205                         borrowBook();
206                     }else if(choice==5) {
207                         returnBook();
208                     }else if(choice==6) {
209                         continue;
210                     }
211                 }
212                 
213             }
214         }
215 
216     }
217 
218 }

 

posted on 2020-06-12 20:59  cherry_ning  阅读(83)  评论(0)    收藏  举报

导航