Java 练习题

1.设计一个方法,输入三个数字并输出最大整数

 

 1  import java.util.Scanner;
 2  3  public class Demo01MaxNumber {
 4      public static void main(String[] args) {
 5          Scanner sc = new Scanner(System.in);
 6          System.out.print("请输入三个整数:");
 7          int a = sc.nextInt();
 8          int b = sc.nextInt();
 9          int c = sc.nextInt();
10          int maxNumber = maxNum(a,b,c);
11          System.out.println("其中最大的数字为");
12          System.out.print(maxNumber);
13      }
14      public static int maxNum(int one,int two,int three){
15          // 单独判断
16         /* if (one >= two && one >= three)
17              return  one;
18          else if (two >= three)
19              return two;
20          else
21              return three;*/
22          // 连续判断
23         /* int max = 0;
24          if (one > two)
25              max = one;
26          else
27              max = two;
28          if (max > three)
29              return max;
30          else
31              return three;*/
32          // 连续判断转化为三元
33          /*int max = one > two ? one:two;
34          max = max > three ? max:three;
35          return max;*/
36          // 另一种
37          int max = one;
38          if (two > max)
39              max = two;
40          if (three > max)
41              max = three;
42          return max;
43      }
44  }

 

2.设计一个程序,输入月份和年份,打印出此年月的天数

 

 1 import java.util.Scanner;
 2 
 3 public class Demo02YearMontheDate {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         System.out.print("请输入年份");
 7         int year = sc.nextInt();
 8         System.out.print("请输入月份");
 9         int month = sc.nextInt();
10         System.out.println(year+"年"+month+"月,有"+isDay(year,month)+"天");
11     }
12     public static int isDay(int year,int month){
13         int currentDays = 0;
14         // 数组 写法  与 if else 写法 大同小异
15         /*int[] runDays = {31,29,31,30,31,30,31,30,31,31,30,31};
16         int[] pingDays = {31,28,31,30,31,30,31,30,31,31,30,31};
17         // 判断 是否是闰年 ,若为闰年则天数加一天
18         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
19             //计算 i从零开始,下标小于你输入的月份,在数组取出此月天数   也可用为 i < month
20             for (int i = 0; i <= month - 1; i++)
21                 currentDays=runDays[i];
22         }else {
23             for (int i = 0; i <= month - 1; i++)
24                 currentDays=pingDays[i];
25         }
26         return currentDays;*/
27         // switch 写法
28         switch (month){
29             case 1: case 3: case 5:case 7:case 9: case 10:case 12:
30                 currentDays = 31;
31                 break;
32             case 4: case 6: case 8:case 11:
33                 currentDays = 30;
34                 break;
35             case 2:
36                 if(year%4==0&&year%100!=0||year%400==0)
37                     currentDays = 29;
38                 else
39                     currentDays = 28;
40                 break;42         }
43         return currentDays;
44     }
45 }

 3.输入的非零三位数字依次输出该数字的百位和十位数,个位数。

 

 1 //输入的非零三位数字依次输出该数字的百位和十位数,个位数
 2 
 3 import java.util.Scanner;
 4 
 5 public class Demo02Digital {
 6     public static void main(String[] args) {
 7         System.out.print("请输入一个三位数的整数:");
 8         int number = new Scanner(System.in).nextInt();
 9         digitalNum(number);
10     }
11     public static void digitalNum(int num){
12         if (num > 0 ){
13             int ge = num % 10;
14             int shi = (num / 10) % 10;
15             int bai = num / 100;
16             System.out.print("个位数是"+ge+",十位数是"+shi+",百位数是"+bai+"。\n");
17         }else {
18             int ge = num % 10;
19             int shi = (num / 10) % 10;
20             int bai = num / 100;
21             System.out.format("个位数是%d,十位数是%d,百位数是%d。\n",-ge,-shi,-bai);
22         }
23 
24     }
25 }

 4.使用面向对象程序设计思想编写一个bankAccount类,该类具有属性:卡号,余额,持卡人姓名,密码。依据属性特征,设置相关属性的类型及访问权限;在类中提供构造方法完成对象的初始化并提供三个行为:显示余额,取款,存款。

 

 1 import java.util.Random;
 2 
 3
4 // 使用面向对象程序设计思想编写一个bankAccount类,该类具有属性:卡号,余额,持卡人姓名,密码。 5 // 依据属性特征,设置相关属性的类型及访问权限; 6 // 在类中提供构造方法完成对象的初始化并提供三个行为:显示余额,取款,存款。 7 public class BankAccount { 8 // fields 9 private String card; 10 private double balance; 11 private String name; 12 private String password; 13 14 // methods 15 // 自动生成卡号 16 public void setCard(){ 17 Random ran = new Random(); 18 String idCard=""; 19 for (int i = 0; i < 9; i++) { 20 int a =ran.nextInt(16); 21 idCard=idCard + a; 22 } 23 card = idCard; 24 } 25 // 获取卡号 26 public String getCard(){ 27 return card; 28 } 29 // 设置余额 30 public void setBalance(double a){ 31 if (a > 0) 32 balance = a; 33 } 34 // 获取余额 显示余额 35 public double getBalance(){ 36 return balance; 37 } 38 // 设置姓名 39 public void setName(String a){ 40 name = a; 41 } 42 // 获取姓名 43 public String getName(){ 44 return name; 45 } 46 // 设置密码 47 public void setPassword(String a){ 48 password = a; 49 } 50 // 获取密码 51 public String getPassword(){ 52 return password; 53 } 54 // 修改密码 55 public boolean changePassword(String a){ 56 if (a.length()>0) 57 password = a; 58 return true; 59 } 60 // 存款 61 public boolean increaseBalance(double a){ 62 if (a>0){ 63 balance += a; 64 return true; 65 } 66 return false; 67 } 68 // 取款 69 public boolean decreaseBalance(double a){ 70 if (a>0 && balance > a){ 71 balance -= a; 72 return true; 73 } 74 return false; 75 } 76 }

 

测试:

 

 1 public class Demo08BankAccount {
 2     public static void main(String[] args) {
 3         BankAccount a = new BankAccount();
 4         a.setCard();
 5         System.out.println(a.getCard());
 6         a.setName("大嘟嘟");
 7         a.setBalance(89533.2);
 8         a.setPassword("123456789");
 9         System.out.println("持卡人姓名:"+a.getName()+"\n卡号:"+a.getCard()+"\n余额:"+a.getBalance()+"\n密码:"+a.getPassword());
10         a.changePassword("");
11         System.out.println("修改后的密码:"+a.getPassword());
12         a.increaseBalance(0.8);
13         System.out.println("充值后的余额:"+a.getBalance());
14         a.decreaseBalance(800000);
15         System.out.println("花费后的余额:"+a.getBalance());
16     }
17 }
posted @ 2022-10-16 16:13  雨泽_mind  阅读(38)  评论(0)    收藏  举报