该文章Github地址:https://github.com/AntonyCheng/java-notes
在此介绍一下作者开源的SpringBoot项目初始化模板(Github仓库地址:https://github.com/AntonyCheng/spring-boot-init-template & CSDN文章地址:https://blog.csdn.net/AntonyCheng/article/details/136555245),该模板集成了最常见的开发组件,同时基于修改配置文件实现组件的装载,除了这些,模板中还有非常丰富的整合示例,同时单体架构也非常适合SpringBoot框架入门,如果觉得有意义或者有帮助,欢迎Star & Issues & PR!
上一章:由浅到深认识Java语言(25):正则表达式
38.阶段性练习
练习一:
编写一个程序,用于接收用户输入的五个数,并计算这五个数的平均数,最后将计算结果返回;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
double[] arr = new double[5];
double pjs = method(arr);
System.out.println("你输入的五个数的平均数是:"+pjs);
}
public static double method(double[] arr) {
Scanner sc = new Scanner(System.in);
double sum = 0;
System.out.println("请输入五个数,用空格分开:");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextDouble();
sum += arr[i];
}
sc.close();
return sum/5;
}
}
打印效果如下:
练习二:
编写一个函数,用于接收三角形的底和高,并计算该三角形的面积,将面积返回;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的三角形的底和高(用空格隔开):");
double floor = sc.nextDouble();
double high = sc.nextDouble();
sc.close();
double area = method(floor,high);
System.out.println("你的三角形面积是:"+area);
}
public static double method(double floor,double high) {
return (floor*high)/2;
}
}
打印效果如下:
练习三:
编写一个函数,用于接收 3 到 10 之间的一个数,然后输出由星号组成的长方形;
例如:用户输入 4 ,输出结果如下:
****
* *
* *
****
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的图像行数:");
int num = sc.nextInt();
sc.close();
method(num);
}
public static void method(int n) {
for (int i = 0; i < n; i++) {
if (i == 0 || i == n-1) {
for (int j = 0; j < n; j++) {
System.out.print("*");
}
}else {
for (int j = 0; j < n; j++) {
if(j == 0||j==n-1) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
打印效果如下:
练习四:
写一个方法 boolean checkIsOddNumber(int num) 判断输入是否为奇数;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个需要判断的数:");
int num = sc.nextInt();
boolean checkIsOddNumber = checkIsOddNumber(num);
System.out.println(checkIsOddNumber);
}
public static boolean checkIsOddNumber(int num) {
if (num % 2 == 0) {
return false;
} else {
return true;
}
}
}
打印效果如下:
练习五:
写一个方法 boolean checkIsPrimeNumber(int num) 判断输入是否为素数;
方法一:
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个需要判断的数:");
int num = sc.nextInt();
boolean checkIsPrimeNumber = checkIsPrimeNumber(num);
System.out.println(checkIsPrimeNumber);
}
public static boolean checkIsPrimeNumber(int num) {
if(num<=1) {
return false;
}else {
int count = 0;
for (int i = 2; i <= num; i++) {
if(num%i != 0) {
count++;
}
}
if(count == num-2) {
return true;
}else {
return false;
}
}
}
}
方法二:
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个需要判断的数:");
int num = sc.nextInt();
boolean checkIsPrimeNumber = checkIsPrimeNumber(num);
System.out.println(checkIsPrimeNumber);
}
public static boolean checkIsPrimeNumber(int num) {
if (num <= 1) {
return false;
} else {
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return false;
}
}
}
return true;
}
}
打印效果如下:
练习六:
打印一棵圣诞树;
import java.util.Iterator;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
method();
}
public static void method() {
for (int i = 1; i <= 15; i++) {
if (i <= 5) {
for (int j = 0; j < 8 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
} else if (i <= 11) {
for (int j = 0; j < 11-i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2*i-7; j++) {
System.out.print("*");
}
System.out.println();
} else {
for (int j = 0; j < 6; j++) {
System.out.print(" ");
}
for (int j = 0; j < 3; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
打印效果如下:
练习七:
用 Java 代码将数组元素顺序颠倒;
import java.util.Arrays;
public class Demo {
public static void main(String[] args) {
int arr[] = {2,3,4,5,6,7};
int[] method = method(arr);
System.out.println(Arrays.toString(method));
}
public static int[] method(int[] arr) {
int n = arr.length;
for (int i = 0; i < arr.length/2; i++) {
int temp = arr[i];
n=n-1;
arr[i] = arr[n];
arr[n] = temp;
}
return arr;
}
}
打印效果如下:
练习八:
写一个名为 Account 的类模拟账户,该类的属性和方法如下图所示:
该类包括的属性:账号 id ,余额 balance , 年利率 annualInterestRate;
包含的方法:访问器方法(getter() 和 setter()),取款方法 withdraw() ,存款方法 deposit() ;
注意取款方法应该判断用户余额是否满足取款条件;
再写一个 Customer 的类模拟客户,该类的属性和方法如下图所示:
该类包括的属性:姓 firstName ,名 lastName 和账号 account;
声明一个公有构造器,这个构造器带有两个代表姓名的参数;
声明两个公有存取器来访问该对象的属性;
以上为基础,写一个测试程序:
-
创建一个Customer,名字叫 Xiao Wu ,他有一个账号为 1900,余额为2000,年利率为 1.23% 的账户。
-
对 Xiao Wu操作:
存入 100 元,再取出 960 元,再取出 2000 元,然后打印 Xiao Wu 的基本信息;
信息:存入成功!100.0元;取出成功!960.0元;余额不足取款失败;Customer [Xiao,Wu] has a account: id is 1900,annualInterestRate is 1.23%,balance is 1140.0;
package top.sharehome.Bag;
public class Demo {
public static void main(String[] args) {
Customer c = new Customer("Xiao", "Wu", new Account(1900, 2000, 1.23));
Account a = c.getAccount();
a.deposit(100);
a.withdraw(960);
a.withdraw(2000);
System.out.println(a.getBalance());
String string = c.toString(a.getAnnualInterestRate(), a.getBalance(), a.getId());
System.out.println(string);
}
}
class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account() {
super();
// TODO Auto-generated constructor stub
}
public Account(int id, double balance, double annualInterestRate) {
super();
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public void withdraw(double amount) {
if (amount > this.balance) {
System.out.println("余额不足取款失败");
} else {
this.balance = this.balance - amount;
System.out.println("取出成功!" + amount + "元");
}
}
public void deposit(double amount) {
this.balance += amount;
System.out.println("存入成功!" + amount + "元");
}
}
class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer(String firstName, String lastName, Account account) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.account = account;
}
public Customer() {
super();
// TODO Auto-generated constructor stub
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String toString(double annualInterestRate, double balance, double id) {
return "Customer [" + firstName + ", " + lastName + "] has a account: id is " + account.getId()
+ " , annualInterestRate is \n" + annualInterestRate + "% , balance is " + balance;
}
}
打印效果如下:
练习九:
统计 “a12 12bsk1928bc” 中的数字字符的数量;
方法一:运用字符在底层是数字存储,进行判断识别;
import java.util.Arrays;
public class Demo {
public static void main(String[] args) {
String str = "a12 12bsk1928bc";
int number = isNumber(str);
System.out.println(number);
}
public static int isNumber(String str) {
char[] charArray = str.toCharArray();
int count = 0;
for (int i = 0; i < charArray.length; i++) {
if (charArray[i]>='0'&&charArray[i]<='9') {
count++;
}
}
return count;
}
}
打印效果如下:
方法二:运用 Character 中 toString 方法将 str 转换成的字符数组转成 String 类型,然后正则表达式;
import java.util.Arrays;
public class Demo {
public static void main(String[] args) {
String str = "a12 12bsk1928bc";
int number = isNumber(str);
System.out.println(number);
}
public static int isNumber(String str) {
char[] charArray = str.toCharArray();
int count = 0;
for (int i = 0; i < charArray.length; i++) {
String string = Character.toString(charArray[i]);
if (string.matches("\\d")) {
count++;
}
}
return count;
}
}
打印效果如下: