Add 代码库2

master
munvls9gi 3 years ago
parent 4d0ddba6a7
commit 4cc9f83f8a

@ -0,0 +1,289 @@
第1题
import java.util.Scanner;
/**
* 任务求N以内最大的10个素数之和。
*/
public class Educode {
public static void main(String[] args) {
// Input
Scanner input = new Scanner(System.in);
int n = input.nextInt();
// Caculate
int count = 0, sum = 0;
int i, j;
for (i = n; i >= 2; i--) {
for (j = 2; j < i; j++) {
if (i % j == 0)
break;
}
if (i == j) {
sum += i;
count++;
if (count == 10)
break;
}
}
// Output
System.out.println(sum);
}
}
第2题
import java.util.Scanner;
/**
* 主程序类 Educode
*/
public class Educode {
public static void main(String[] args) {
// Input
Scanner in = new Scanner(System.in);
int id = in.nextInt();
String name = in.next();
double balance = in.nextDouble();
double annualInterestRate = in.nextDouble();
// Caculate
Account account = new Account(id, name, balance, annualInterestRate);
account.withdraw(2500);
account.deposit(3000);
// Output
account.print();
}
}
// 银行账户类 Account
class Account {
/********** Begin **********/
// 4个私有属性 id name balance annualInterestRate
private int id;
private String name;
private double balance;
private double annualInterestRate;
// 有参构造方法
public Account(int id, String name, double balance, double annualInterestRate) {
this.id = id;
this.name = name;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
// Setter & Getter 方法
public void setID(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public int getID() {
return id;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
// 业务方法 withdraw(double amount),从账户中取特定数额的款。
public boolean withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
return true;
} else
return false;
}
// 业务方法 deposit(double amount),往账户中存特定数额的款。
public void deposit(double amount) {
balance += amount;
}
// 业务方法 getMonthlyInterestRate(),返回月利率
public double getMonthlyInterestRate() {
return annualInterestRate / 1200;
}
// 业务方法 print(),输出账户信息(账号、姓名、存款金额、月利率)
public void print() {
System.out.println(id);
System.out.println(name);
System.out.println(balance);
System.out.println(getMonthlyInterestRate() + "%");
}
/********** End **********/
}
第3题
import java.util.Scanner;
/**
* 主程序类 TestAnimal
*/
public class TestAnimal {
public static void main(String[] args) {
// Input
Scanner input = new Scanner(System.in);
int n = input.nextInt(); //动物个数
Animal as[] = new Animal[n];
String name; // 动物名称 ("Dog"、"Cat"、"Wolf")
/********** Begin **********/
// 输入 n 个具体动物 (Dog、Cat、Wolf)
for (int i = 0; i < n; i++) {
name = input.next();
if (name.equals("Dog"))
as[i] = new Dog();
else if (name.equals("Cat"))
as[i] = new Cat();
else if (name.equals("Wolf"))
as[i] = new Wolf();
}
//调用 as 数组中所有动物的 eat()方法
for (int i = 0; i < as.length; i++) {
as[i].eat();
}
//调用 as 数组中所有宠物的 play()方法
for (int i = 0; i < as.length; i++) {
if (as[i] instanceof Pet) {
Pet p = (Pet) as[i];
p.play();
}
}
/********** End **********/
}
}
abstract class Animal {
public abstract void eat();
}
interface Pet {
void play();
}
/*************** Begin ***************/
// Dog 类,继承 Animal类实现 Pet接口
class Dog extends Animal implements Pet {
public void eat() {
System.out.println("Dog eat bones");
}
public void play() {
System.out.println("Play with Dog");
}
}
// Cat 类,继承 Animal类实现 Pet接口
class Cat extends Animal implements Pet {
public void eat() {
System.out.println("Cat eat fish");
}
public void play() {
System.out.println("Play with Cat");
}
}
// Wolf 类,继承 Animal类
class Wolf extends Animal {
public void eat() {
System.out.println("Wolf eat meat");
}
}
/*************** End ***************/
第4题
import java.util.Scanner;
/******** Begin ********/
// 任务1 - 使用继承 Thread 类的方式,定义线程类 MyThread1
class MyThread1 extends Thread {
// 私有属性 n
private int n;
// 有参构造方法
public MyThread1(int n) {
this.n = n;
}
// 重写 run()方法 -- 每次输出一个整数值后 sleep(400) 注意sleep要捕获 InterruptedException异常
public void run() {
for (int i = 1; i <= n; i++) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 任务2 - 使用实现 Runnable 接口的方式,定义线程类 MyThread2
class MyThread2 implements Runnable {
// 私有属性 n
private int n;
// 有参构造方法
public MyThread2(int n) {
this.n = n;
}
// 重写 run()方法 -- 每次输出一个小写字母后 sleep(400) 注意sleep要捕获 InterruptedException异常
public void run() {
for (int i = 0; i < n; i++) {
System.out.println(Thread.currentThread().getName() + ": " + (char) ('z' - i));
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/******** End ********/
/**
* 主程序类 EduCode
*/
public class EduCode {
public static void main(String[] args) {
// 1-输入 n
Scanner input = new Scanner(System.in);
int n = input.nextInt();
// 2-创建线程1
MyThread1 t1 = new MyThread1(n);
t1.setName("线程1");
t1.setPriority(Thread.MIN_PRIORITY);
// 3-创建线程2
Thread t2 = new Thread(new MyThread2(n), "线程2");
t2.setPriority(Thread.MAX_PRIORITY);
// 4-启动线程
/******** Begin ********/
t1.start();
t2.start();
/******** End ********/
}
}
Loading…
Cancel
Save