mirror of
https://github.com/Dichgrem/Java.git
synced 2025-12-16 12:41:58 -05:00
style:fmt
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
com/study/test/
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.study.demo;
|
||||
|
||||
public class Demo {
|
||||
public static void main(String [] args) {
|
||||
System.out.println("HelloWorld!");
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
System.out.println("HelloWorld!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package com.study.demo;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
// I am 212306206
|
||||
public class test1_type {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner (System.in);
|
||||
int a = scanner.nextInt();
|
||||
int b = scanner.nextInt();
|
||||
int sum = a + b;
|
||||
System.out.println(a + " + " + b + " = " + sum);
|
||||
scanner.close();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int a = scanner.nextInt();
|
||||
int b = scanner.nextInt();
|
||||
int sum = a + b;
|
||||
System.out.println(a + " + " + b + " = " + sum);
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package com.study.demo;
|
||||
|
||||
public class test2_for {
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = new int[5];
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
numbers[i] = i * 10;
|
||||
}
|
||||
|
||||
System.out.print("数组内容: ");
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
System.out.print(numbers[i] + " ");
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = new int[5];
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
numbers[i] = i * 10;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.print("数组内容: ");
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
System.out.print(numbers[i] + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
package com.study.demo;
|
||||
|
||||
public class test3_1_99 {
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = new int[99];
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = new int[99];
|
||||
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
numbers[i] = i + 1;
|
||||
}
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
sum += numbers[i];
|
||||
}
|
||||
|
||||
System.out.println("1+2+...+99 的和 = " + sum);
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
numbers[i] = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
sum += numbers[i];
|
||||
}
|
||||
|
||||
System.out.println("1+2+...+99 的和 = " + sum);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package com.study.demo;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test5 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int a = scanner.nextInt();
|
||||
int b = scanner.nextInt();
|
||||
int c = scanner.nextInt();
|
||||
double pi = 3.1415926535;
|
||||
double area = a * b * c + a * c + (pi * c * c) / 4;
|
||||
System.out.printf("%.3f\n", area / 10000);
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int a = scanner.nextInt();
|
||||
int b = scanner.nextInt();
|
||||
int c = scanner.nextInt();
|
||||
double pi = 3.1415926535;
|
||||
double area = a * b * c + a * c + (pi * c * c) / 4;
|
||||
System.out.printf("%.3f\n", area / 10000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ package com.study.demo;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test6 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int result = 1 << n;
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int result = 1 << n;
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ package com.study.demo;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test7 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int x = scanner.nextInt();
|
||||
int y = scanner.nextInt();
|
||||
int startYear = 2016, startMonth = 8;
|
||||
int totalMonths = (x - startYear) * 12 + y - startMonth + 1;
|
||||
System.out.println(totalMonths);
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int x = scanner.nextInt();
|
||||
int y = scanner.nextInt();
|
||||
int startYear = 2016, startMonth = 8;
|
||||
int totalMonths = (x - startYear) * 12 + y - startMonth + 1;
|
||||
System.out.println(totalMonths);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,17 @@ package com.study.demo;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test8 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int x = scanner.nextInt();
|
||||
int k = scanner.nextInt();
|
||||
int a = x / 1000 % k;
|
||||
int b = x / 100 % 10 % k;
|
||||
int c = x / 10 % 10 % k;
|
||||
int d = x % 10 % k;
|
||||
System.out.print(a);
|
||||
System.out.print(b);
|
||||
System.out.print(c);
|
||||
System.out.println(d);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int x = scanner.nextInt();
|
||||
int k = scanner.nextInt();
|
||||
int a = x / 1000 % k;
|
||||
int b = x / 100 % 10 % k;
|
||||
int c = x / 10 % 10 % k;
|
||||
int d = x % 10 % k;
|
||||
System.out.print(a);
|
||||
System.out.print(b);
|
||||
System.out.print(c);
|
||||
System.out.println(d);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,20 +3,20 @@ package com.study.demo;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test3_1 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int[] arr = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[i] = scanner.nextInt();
|
||||
}
|
||||
for (int i = 0; i < n / 2; i++) {
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[n - 1 - i];
|
||||
arr[n - 1 - i] = temp;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.print(arr[i] + (i == n - 1 ? "" : " "));
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int[] arr = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[i] = scanner.nextInt();
|
||||
}
|
||||
for (int i = 0; i < n / 2; i++) {
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[n - 1 - i];
|
||||
arr[n - 1 - i] = temp;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
System.out.print(arr[i] + (i == n - 1 ? "" : " "));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,21 +3,21 @@ package com.study.demo;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test3_2 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int[] a = new int[n];
|
||||
int[] b = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
a[i] = scanner.nextInt();
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
b[i] = scanner.nextInt();
|
||||
}
|
||||
int sum = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
sum += a[i] * b[i];
|
||||
}
|
||||
System.out.println(sum);
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int[] a = new int[n];
|
||||
int[] b = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
a[i] = scanner.nextInt();
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
b[i] = scanner.nextInt();
|
||||
}
|
||||
int sum = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
sum += a[i] * b[i];
|
||||
}
|
||||
System.out.println(sum);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,23 +3,23 @@ package com.study.demo;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test3_3 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int[] arr = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[i] = scanner.nextInt();
|
||||
}
|
||||
int maxLen = 1, currentLen = 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (arr[i] == arr[i - 1]) {
|
||||
currentLen++;
|
||||
} else {
|
||||
maxLen = Math.max(maxLen, currentLen);
|
||||
currentLen = 1;
|
||||
}
|
||||
}
|
||||
maxLen = Math.max(maxLen, currentLen);
|
||||
System.out.println(maxLen);
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int[] arr = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[i] = scanner.nextInt();
|
||||
}
|
||||
int maxLen = 1, currentLen = 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (arr[i] == arr[i - 1]) {
|
||||
currentLen++;
|
||||
} else {
|
||||
maxLen = Math.max(maxLen, currentLen);
|
||||
currentLen = 1;
|
||||
}
|
||||
}
|
||||
maxLen = Math.max(maxLen, currentLen);
|
||||
System.out.println(maxLen);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,21 +3,21 @@ package com.study.demo;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test3_4 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int m = scanner.nextInt();
|
||||
int[][] image = new int[n][m];
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
image[i][j] = scanner.nextInt();
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < m; j++) {
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
System.out.print(image[i][j] + (i == 0 ? "" : " "));
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int m = scanner.nextInt();
|
||||
int[][] image = new int[n][m];
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
image[i][j] = scanner.nextInt();
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < m; j++) {
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
System.out.print(image[i][j] + (i == 0 ? "" : " "));
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,32 +3,32 @@ package com.study.demo;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class test3_5 {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int[] t = new int[n - 1];
|
||||
int[] s = new int[n];
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
t[i] = scanner.nextInt();
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
s[i] = scanner.nextInt();
|
||||
}
|
||||
int x = scanner.nextInt();
|
||||
int y = scanner.nextInt();
|
||||
|
||||
int total = 0;
|
||||
if (x < y) {
|
||||
for (int i = x - 1; i < y - 1; i++) {
|
||||
total += s[i] + t[i];
|
||||
}
|
||||
total += s[y - 1];
|
||||
} else {
|
||||
for (int i = x - 1; i >= y; i--) {
|
||||
total += s[i] + (i > 0 ? t[i - 1] : 0);
|
||||
}
|
||||
total += s[y - 1];
|
||||
}
|
||||
System.out.println(total);
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int n = scanner.nextInt();
|
||||
int[] t = new int[n - 1];
|
||||
int[] s = new int[n];
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
t[i] = scanner.nextInt();
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
s[i] = scanner.nextInt();
|
||||
}
|
||||
int x = scanner.nextInt();
|
||||
int y = scanner.nextInt();
|
||||
|
||||
int total = 0;
|
||||
if (x < y) {
|
||||
for (int i = x - 1; i < y - 1; i++) {
|
||||
total += s[i] + t[i];
|
||||
}
|
||||
total += s[y - 1];
|
||||
} else {
|
||||
for (int i = x - 1; i >= y; i--) {
|
||||
total += s[i] + (i > 0 ? t[i - 1] : 0);
|
||||
}
|
||||
total += s[y - 1];
|
||||
}
|
||||
System.out.println(total);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Account {
|
||||
private String accountId;
|
||||
private String owner;
|
||||
private double balance;
|
||||
private String accountId;
|
||||
private String owner;
|
||||
private double balance;
|
||||
|
||||
public Account(String accountId, String owner, double balance) {
|
||||
this.accountId = accountId;
|
||||
this.owner = owner;
|
||||
this.balance = balance;
|
||||
}
|
||||
public Account(String accountId, String owner, double balance) {
|
||||
this.accountId = accountId;
|
||||
this.owner = owner;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public void deposit(double amount) {
|
||||
if (amount > 0) {
|
||||
balance += amount;
|
||||
}
|
||||
public void deposit(double amount) {
|
||||
if (amount > 0) {
|
||||
balance += amount;
|
||||
}
|
||||
}
|
||||
|
||||
public void withdraw(double amount) {
|
||||
if (amount > 0 && amount <= balance) {
|
||||
balance -= amount;
|
||||
}
|
||||
public void withdraw(double amount) {
|
||||
if (amount > 0 && amount <= balance) {
|
||||
balance -= amount;
|
||||
}
|
||||
}
|
||||
|
||||
public void transfer(Account target, double amount) {
|
||||
if (amount > 0 && amount <= balance) {
|
||||
this.balance -= amount;
|
||||
target.balance += amount;
|
||||
}
|
||||
public void transfer(Account target, double amount) {
|
||||
if (amount > 0 && amount <= balance) {
|
||||
this.balance -= amount;
|
||||
target.balance += amount;
|
||||
}
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
}
|
||||
public double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Account acc1 = new Account("A001", "张三", 1000);
|
||||
Account acc2 = new Account("A002", "李四", 500);
|
||||
|
||||
acc1.deposit(200);
|
||||
acc1.withdraw(100);
|
||||
acc1.transfer(acc2, 300);
|
||||
|
||||
System.out.println("账户1余额:" + acc1.getBalance());
|
||||
System.out.println("账户2余额:" + acc2.getBalance());
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Account acc1 = new Account("A001", "张三", 1000);
|
||||
Account acc2 = new Account("A002", "李四", 500);
|
||||
|
||||
acc1.deposit(200);
|
||||
acc1.withdraw(100);
|
||||
acc1.transfer(acc2, 300);
|
||||
|
||||
System.out.println("账户1余额:" + acc1.getBalance());
|
||||
System.out.println("账户2余额:" + acc2.getBalance());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,45 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public abstract class Animal {
|
||||
protected String name;
|
||||
protected String name;
|
||||
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public abstract void makeSound();
|
||||
public abstract void move();
|
||||
public abstract void makeSound();
|
||||
|
||||
public abstract void move();
|
||||
}
|
||||
|
||||
class Dog extends Animal {
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeSound() {
|
||||
System.out.println(name + " 汪汪叫");
|
||||
}
|
||||
@Override
|
||||
public void makeSound() {
|
||||
System.out.println(name + " 汪汪叫");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
System.out.println(name + " 跑动");
|
||||
}
|
||||
@Override
|
||||
public void move() {
|
||||
System.out.println(name + " 跑动");
|
||||
}
|
||||
}
|
||||
|
||||
class Bird extends Animal {
|
||||
public Bird(String name) {
|
||||
super(name);
|
||||
}
|
||||
public Bird(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeSound() {
|
||||
System.out.println(name + " 叽叽喳喳");
|
||||
}
|
||||
@Override
|
||||
public void makeSound() {
|
||||
System.out.println(name + " 叽叽喳喳");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void move() {
|
||||
System.out.println(name + " 飞翔");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void move() {
|
||||
System.out.println(name + " 飞翔");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Animal dog = new Dog("Buddy");
|
||||
Animal bird = new Bird("Tweety");
|
||||
dog.makeSound();
|
||||
dog.move();
|
||||
bird.makeSound();
|
||||
bird.move();
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Animal dog = new Dog("Buddy");
|
||||
Animal bird = new Bird("Tweety");
|
||||
dog.makeSound();
|
||||
dog.move();
|
||||
bird.makeSound();
|
||||
bird.move();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +1,38 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Rectangle {
|
||||
private double width = 1.0;
|
||||
private double height = 1.0;
|
||||
private double width = 1.0;
|
||||
private double height = 1.0;
|
||||
|
||||
public Rectangle() {}
|
||||
public Rectangle() {
|
||||
}
|
||||
|
||||
public Rectangle(double width, double height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
public Rectangle(double width, double height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public double getArea() {
|
||||
return width * height;
|
||||
}
|
||||
public double getArea() {
|
||||
return width * height;
|
||||
}
|
||||
|
||||
public double getPerimeter() {
|
||||
return 2 * (width + height);
|
||||
}
|
||||
public double getPerimeter() {
|
||||
return 2 * (width + height);
|
||||
}
|
||||
|
||||
public double getWidth() {
|
||||
return width;
|
||||
}
|
||||
public double getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(double width) {
|
||||
this.width = width;
|
||||
}
|
||||
public void setWidth(double width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public double getHeight() {
|
||||
return height;
|
||||
}
|
||||
public double getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(double height) {
|
||||
this.height = height;
|
||||
}
|
||||
public void setHeight(double height) {
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Rectangle rect1 = new Rectangle(5.0, 3.0);
|
||||
System.out.println("面积:" + rect1.getArea());
|
||||
System.out.println("周长:" + rect1.getPerimeter());
|
||||
|
||||
Rectangle rect2 = new Rectangle();
|
||||
System.out.println("默认面积:" + rect2.getArea());
|
||||
System.out.println("默认周长:" + rect2.getPerimeter());
|
||||
|
||||
rect2.setWidth(2.5);
|
||||
rect2.setHeight(4.0);
|
||||
System.out.println("修改后面积:" + rect2.getArea());
|
||||
System.out.println("修改后周长:" + rect2.getPerimeter());
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Rectangle rect1 = new Rectangle(5.0, 3.0);
|
||||
System.out.println("面积:" + rect1.getArea());
|
||||
System.out.println("周长:" + rect1.getPerimeter());
|
||||
|
||||
Rectangle rect2 = new Rectangle();
|
||||
System.out.println("默认面积:" + rect2.getArea());
|
||||
System.out.println("默认周长:" + rect2.getPerimeter());
|
||||
|
||||
rect2.setWidth(2.5);
|
||||
rect2.setHeight(4.0);
|
||||
System.out.println("修改后面积:" + rect2.getArea());
|
||||
System.out.println("修改后周长:" + rect2.getPerimeter());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,46 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Student {
|
||||
private String id;
|
||||
private String name;
|
||||
private int[] score = new int[10];
|
||||
private String id;
|
||||
private String name;
|
||||
private int[] score = new int[10];
|
||||
|
||||
public Student(String id, String name, int[] scores) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
if (scores.length == 10) {
|
||||
this.score = scores;
|
||||
}
|
||||
public Student(String id, String name, int[] scores) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
if (scores.length == 10) {
|
||||
this.score = scores;
|
||||
}
|
||||
}
|
||||
|
||||
public double getAverage() {
|
||||
int sum = 0;
|
||||
for (int s : score) {
|
||||
sum += s;
|
||||
}
|
||||
return (double) Math.round((sum / 10.0) * 100) / 100;
|
||||
public double getAverage() {
|
||||
int sum = 0;
|
||||
for (int s : score) {
|
||||
sum += s;
|
||||
}
|
||||
return (double) Math.round((sum / 10.0) * 100) / 100;
|
||||
}
|
||||
|
||||
public int getMaxScore() {
|
||||
int max = score[0];
|
||||
for (int s : score) {
|
||||
if (s > max) max = s;
|
||||
}
|
||||
return max;
|
||||
public int getMaxScore() {
|
||||
int max = score[0];
|
||||
for (int s : score) {
|
||||
if (s > max)
|
||||
max = s;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public char getGrade() {
|
||||
double avg = getAverage();
|
||||
if (avg >= 90) return 'A';
|
||||
else if (avg >= 80) return 'B';
|
||||
else if (avg >= 70) return 'C';
|
||||
else if (avg >= 60) return 'D';
|
||||
else return 'E';
|
||||
}
|
||||
}
|
||||
public char getGrade() {
|
||||
double avg = getAverage();
|
||||
if (avg >= 90)
|
||||
return 'A';
|
||||
else if (avg >= 80)
|
||||
return 'B';
|
||||
else if (avg >= 70)
|
||||
return 'C';
|
||||
else if (avg >= 60)
|
||||
return 'D';
|
||||
else
|
||||
return 'E';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package com.study.demo2;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
int[] scores = {85, 90, 78, 92, 88, 76, 95, 89, 79, 94};
|
||||
Student student = new Student("5001", "张三", scores);
|
||||
|
||||
System.out.println("平均分: " + student.getAverage());
|
||||
System.out.println("最高分: " + student.getMaxScore());
|
||||
System.out.println("等级: " + student.getGrade());
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
int[] scores = { 85, 90, 78, 92, 88, 76, 95, 89, 79, 94 };
|
||||
Student student = new Student("5001", "张三", scores);
|
||||
System.out.println("平均分: " + student.getAverage());
|
||||
System.out.println("最高分: " + student.getMaxScore());
|
||||
System.out.println("等级: " + student.getGrade());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.study.demo3;
|
||||
|
||||
public class Bicycle extends Vehicle {
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("The bike is started");
|
||||
}
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("The bike is started");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +1,57 @@
|
||||
package com.study.demo3;
|
||||
|
||||
public class Book {
|
||||
protected String isbn;
|
||||
protected String title;
|
||||
protected boolean available;
|
||||
protected String isbn;
|
||||
protected String title;
|
||||
protected boolean available;
|
||||
|
||||
|
||||
public Book() {
|
||||
this.available = true;
|
||||
}
|
||||
public Book() {
|
||||
this.available = true;
|
||||
}
|
||||
|
||||
|
||||
public Book(String isbn, String title) {
|
||||
this.isbn = isbn;
|
||||
this.title = title;
|
||||
this.available = true;
|
||||
}
|
||||
public Book(String isbn, String title) {
|
||||
this.isbn = isbn;
|
||||
this.title = title;
|
||||
this.available = true;
|
||||
}
|
||||
|
||||
|
||||
public Book(String isbn, String title, boolean available) {
|
||||
this.isbn = isbn;
|
||||
this.title = title;
|
||||
this.available = available;
|
||||
}
|
||||
public Book(String isbn, String title, boolean available) {
|
||||
this.isbn = isbn;
|
||||
this.title = title;
|
||||
this.available = available;
|
||||
}
|
||||
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public void setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
public void setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public boolean isAvailable() {
|
||||
return available;
|
||||
}
|
||||
public boolean isAvailable() {
|
||||
return available;
|
||||
}
|
||||
|
||||
public void setAvailable(boolean available) {
|
||||
this.available = available;
|
||||
}
|
||||
public void setAvailable(boolean available) {
|
||||
this.available = available;
|
||||
}
|
||||
|
||||
|
||||
public void returnBook() {
|
||||
if (!this.available) {
|
||||
this.available = true;
|
||||
System.out.println(this.title + " (ISBN: " + this.isbn + ") has been returned and is now available.");
|
||||
} else {
|
||||
System.out.println(this.title + " (ISBN: " + this.isbn + ") is already available.");
|
||||
}
|
||||
public void returnBook() {
|
||||
if (!this.available) {
|
||||
this.available = true;
|
||||
System.out.println(
|
||||
this.title + " (ISBN: " + this.isbn + ") has been returned and is now available.");
|
||||
} else {
|
||||
System.out.println(this.title + " (ISBN: " + this.isbn + ") is already available.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.study.demo3;
|
||||
|
||||
public class Car extends Vehicle {
|
||||
@Override
|
||||
|
||||
public void start() {
|
||||
System.out.println("The car is started");
|
||||
}
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("The car is started");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,35 @@
|
||||
package com.study.demo3;
|
||||
|
||||
public class EBook extends Book {
|
||||
private double fileSize;
|
||||
private double fileSize;
|
||||
|
||||
|
||||
public EBook() {
|
||||
super();
|
||||
}
|
||||
public EBook() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public EBook(String isbn, String title, double fileSize) {
|
||||
super(isbn, title);
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
public EBook(String isbn, String title, double fileSize) {
|
||||
super(isbn, title);
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
|
||||
|
||||
public double getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
public double getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
public void setFileSize(double fileSize) {
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
public void setFileSize(double fileSize) {
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EBook [ISBN=" + isbn + ", Title=" + title + ", FileSize=" + fileSize + "MB, Available=" + available + "]";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EBook [ISBN="
|
||||
+ isbn
|
||||
+ ", Title="
|
||||
+ title
|
||||
+ ", FileSize="
|
||||
+ fileSize
|
||||
+ "MB, Available="
|
||||
+ available
|
||||
+ "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,37 @@
|
||||
package com.study.demo3;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Library {
|
||||
|
||||
private Map<String, Book> books;
|
||||
|
||||
public Library() {
|
||||
this.books = new HashMap<>();
|
||||
private Map<String, Book> books;
|
||||
|
||||
public Library() {
|
||||
this.books = new HashMap<>();
|
||||
}
|
||||
|
||||
public void addBook(Book b) {
|
||||
String isbn = b.getIsbn();
|
||||
|
||||
if (books.containsKey(isbn)) {
|
||||
System.out.println("The book already exists, adding it failed.");
|
||||
} else {
|
||||
books.put(isbn, b);
|
||||
System.out.println("Book added successfully: " + b.getTitle());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void addBook(Book b) {
|
||||
String isbn = b.getIsbn();
|
||||
public List<Book> searchAvailableBooks() {
|
||||
List<Book> availableBooks = new ArrayList<>();
|
||||
|
||||
|
||||
if (books.containsKey(isbn)) {
|
||||
System.out.println("The book already exists, adding it failed.");
|
||||
} else {
|
||||
books.put(isbn, b);
|
||||
System.out.println("Book added successfully: " + b.getTitle());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<Book> searchAvailableBooks() {
|
||||
List<Book> availableBooks = new ArrayList<>();
|
||||
|
||||
|
||||
for (Book book : books.values()) {
|
||||
if (book.isAvailable()) {
|
||||
availableBooks.add(book);
|
||||
}
|
||||
}
|
||||
return availableBooks;
|
||||
for (Book book : books.values()) {
|
||||
if (book.isAvailable()) {
|
||||
availableBooks.add(book);
|
||||
}
|
||||
}
|
||||
return availableBooks;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,39 @@
|
||||
package com.study.demo3;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LibraryTest {
|
||||
public static void main(String[] args) {
|
||||
Library lib = new Library();
|
||||
public static void main(String[] args) {
|
||||
Library lib = new Library();
|
||||
|
||||
|
||||
EBook e1 = new EBook("E001", "Java Basics", 5.2);
|
||||
|
||||
e1.setAvailable(false);
|
||||
EBook e1 = new EBook("E001", "Java Basics", 5.2);
|
||||
|
||||
|
||||
PrintedBook p1 = new PrintedBook("P001", "Design Patterns", 3);
|
||||
e1.setAvailable(false);
|
||||
|
||||
|
||||
lib.addBook(e1);
|
||||
lib.addBook(p1);
|
||||
PrintedBook p1 = new PrintedBook("P001", "Design Patterns", 3);
|
||||
|
||||
|
||||
lib.addBook(p1);
|
||||
lib.addBook(e1);
|
||||
lib.addBook(p1);
|
||||
|
||||
|
||||
System.out.println("\n--- 可借阅图书列表 (available = true) ---");
|
||||
List<Book> availableList = lib.searchAvailableBooks();
|
||||
lib.addBook(p1);
|
||||
|
||||
if (availableList.isEmpty()) {
|
||||
System.out.println("No books are currently available for loan.");
|
||||
} else {
|
||||
for (Book book : availableList) {
|
||||
|
||||
System.out.println("- " + book.getTitle() + " (ISBN: " + book.getIsbn() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
e1.returnBook();
|
||||
System.out.println("\n--- 恢复 e1 后可借阅图书列表 ---");
|
||||
availableList = lib.searchAvailableBooks();
|
||||
for (Book book : availableList) {
|
||||
System.out.println("- " + book.getTitle() + " (ISBN: " + book.getIsbn() + ")");
|
||||
}
|
||||
System.out.println("\n--- 可借阅图书列表 (available = true) ---");
|
||||
List<Book> availableList = lib.searchAvailableBooks();
|
||||
|
||||
if (availableList.isEmpty()) {
|
||||
System.out.println("No books are currently available for loan.");
|
||||
} else {
|
||||
for (Book book : availableList) {
|
||||
|
||||
System.out.println("- " + book.getTitle() + " (ISBN: " + book.getIsbn() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
e1.returnBook();
|
||||
System.out.println("\n--- 恢复 e1 后可借阅图书列表 ---");
|
||||
availableList = lib.searchAvailableBooks();
|
||||
for (Book book : availableList) {
|
||||
System.out.println("- " + book.getTitle() + " (ISBN: " + book.getIsbn() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,30 @@
|
||||
package com.study.demo3;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println("--- 测试 EBook ---");
|
||||
|
||||
EBook ebook = new EBook("123-456", "Java Programming", 2.5);
|
||||
System.out.println("EBook Status (Initial): " + ebook.toString());
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
ebook.setAvailable(false);
|
||||
System.out.println("EBook Status (Borrowed): " + ebook.toString());
|
||||
System.out.println("--- 测试 EBook ---");
|
||||
|
||||
|
||||
ebook.returnBook();
|
||||
System.out.println("EBook Status (After Return): " + ebook.toString());
|
||||
EBook ebook = new EBook("123-456", "Java Programming", 2.5);
|
||||
System.out.println("EBook Status (Initial): " + ebook.toString());
|
||||
|
||||
|
||||
System.out.println("\n--- 测试 PrintedBook ---");
|
||||
PrintedBook pbook = new PrintedBook();
|
||||
pbook.setIsbn("789-012");
|
||||
pbook.setTitle("Python Guide");
|
||||
pbook.setStock(10);
|
||||
System.out.println("PBook Status (Initial): " + pbook.toString());
|
||||
ebook.setAvailable(false);
|
||||
System.out.println("EBook Status (Borrowed): " + ebook.toString());
|
||||
|
||||
|
||||
pbook.setAvailable(false);
|
||||
System.out.println("PBook Status (Borrowed): " + pbook.toString());
|
||||
ebook.returnBook();
|
||||
System.out.println("EBook Status (After Return): " + ebook.toString());
|
||||
|
||||
|
||||
pbook.returnBook();
|
||||
System.out.println("PBook Status (After Return): " + pbook.toString());
|
||||
}
|
||||
System.out.println("\n--- 测试 PrintedBook ---");
|
||||
PrintedBook pbook = new PrintedBook();
|
||||
pbook.setIsbn("789-012");
|
||||
pbook.setTitle("Python Guide");
|
||||
pbook.setStock(10);
|
||||
System.out.println("PBook Status (Initial): " + pbook.toString());
|
||||
|
||||
pbook.setAvailable(false);
|
||||
System.out.println("PBook Status (Borrowed): " + pbook.toString());
|
||||
|
||||
pbook.returnBook();
|
||||
System.out.println("PBook Status (After Return): " + pbook.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,35 @@
|
||||
package com.study.demo3;
|
||||
|
||||
public class PrintedBook extends Book {
|
||||
private int stock;
|
||||
private int stock;
|
||||
|
||||
|
||||
public PrintedBook() {
|
||||
super();
|
||||
}
|
||||
public PrintedBook() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
public PrintedBook(String isbn, String title, int stock) {
|
||||
super(isbn, title);
|
||||
this.stock = stock;
|
||||
}
|
||||
public PrintedBook(String isbn, String title, int stock) {
|
||||
super(isbn, title);
|
||||
this.stock = stock;
|
||||
}
|
||||
|
||||
|
||||
public int getStock() {
|
||||
return stock;
|
||||
}
|
||||
public int getStock() {
|
||||
return stock;
|
||||
}
|
||||
|
||||
public void setStock(int stock) {
|
||||
this.stock = stock;
|
||||
}
|
||||
public void setStock(int stock) {
|
||||
this.stock = stock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PrintedBook [ISBN=" + isbn + ", Title=" + title + ", Stock=" + stock + ", Available=" + available + "]";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PrintedBook [ISBN="
|
||||
+ isbn
|
||||
+ ", Title="
|
||||
+ title
|
||||
+ ", Stock="
|
||||
+ stock
|
||||
+ ", Available="
|
||||
+ available
|
||||
+ "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
package com.study.demo3;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class StudentManager {
|
||||
public static void main(String[] args) {
|
||||
List<String> studentNames = new ArrayList<>();
|
||||
Map<String, Integer> studentScores = new HashMap<>();
|
||||
public static void main(String[] args) {
|
||||
List<String> studentNames = new ArrayList<>();
|
||||
Map<String, Integer> studentScores = new HashMap<>();
|
||||
|
||||
studentNames.add("张三");
|
||||
studentNames.add("李四");
|
||||
studentNames.add("王五");
|
||||
studentNames.add("张三");
|
||||
studentNames.add("李四");
|
||||
studentNames.add("王五");
|
||||
|
||||
studentScores.put("张三", 85);
|
||||
studentScores.put("李四", 77);
|
||||
studentScores.put("王五", 99);
|
||||
studentScores.put("张三", 85);
|
||||
studentScores.put("李四", 77);
|
||||
studentScores.put("王五", 99);
|
||||
|
||||
System.out.println("--- 所有学生姓名 ---");
|
||||
for (String name : studentNames) {
|
||||
System.out.println(name);
|
||||
}
|
||||
|
||||
studentScores.put("张三", 70);
|
||||
|
||||
Integer zhangsanScore = studentScores.get("张三");
|
||||
System.out.println("\n--- 修改后张三的成绩 ---");
|
||||
System.out.println("张三的成绩: " + zhangsanScore);
|
||||
System.out.println("--- 所有学生姓名 ---");
|
||||
for (String name : studentNames) {
|
||||
System.out.println(name);
|
||||
}
|
||||
|
||||
studentScores.put("张三", 70);
|
||||
|
||||
Integer zhangsanScore = studentScores.get("张三");
|
||||
System.out.println("\n--- 修改后张三的成绩 ---");
|
||||
System.out.println("张三的成绩: " + zhangsanScore);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.study.demo3;
|
||||
|
||||
public class Train extends Vehicle {
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("The train is started");
|
||||
}
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("The train is started");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
package com.study.demo3;
|
||||
|
||||
public class Vehicle {
|
||||
protected String name;
|
||||
protected String name;
|
||||
|
||||
public Vehicle(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Vehicle() {
|
||||
this.name = "Unknown Vehicle";
|
||||
}
|
||||
public Vehicle(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
System.out.println("Vehicle Start");
|
||||
}
|
||||
public Vehicle() {
|
||||
this.name = "Unknown Vehicle";
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
System.out.println("Vehicle Stop");
|
||||
}
|
||||
public void start() {
|
||||
System.out.println("Vehicle Start");
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
System.out.println("Vehicle Stop");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user