Java基礎
練習4:輸入圓的半徑,計算圓的周長和麪積。
package com.lovoinfo;
import java.util.Scanner;
public class CalcCircle {
private static final double PI = 3.14;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入圓的半徑: ");
double radius = sc.nextDouble();
double area = PI * radius * radius;
double circumference = 2 * PI * radius;
System.out.println("周長: " + circumference);
System.out.println("面積: " + area);
sc.close();
}
}
練習5:輸入三個整數,按從小到大的順序輸出。
package com.lovo;
import java.util.Scanner;
public class SortThreeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入三個數: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if(a > b) {
int temp = a;
a = b;
b = temp;
}
if(a > c) {
int temp = a;
a = c;
c = temp;
}
if(b > c) {
int temp = b;
b = c;
c = temp;
}
System.out.printf("%d\t%d\t%d\n", a, b, c);
sc.close();
}
}
作業:輸入三個整數,輸出其中最大的數。
第三天:程序邏輯-1(分支和循環)
- 程序的結構
- 流程圖
- 分支結構
- 循環結構
- 多重循環結構
練習1:分段函數求值。
package com.lovoinfo;
import java.util.Scanner;
public class Fx {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("x = ");
double x = sc.nextDouble();
double y;
if(x < -1) {
y = 3 * x + 5;
}
else if(x <= 1) {
y = x - 1;
}
else {
y = 5 * x - 3;
}
System.out.println("f(x) = " + y);
sc.close();
}
}
練習2:個人所得稅計算。在我國,個人所得稅的起徵點是3500元,計算公式是:
個人所得稅 = (工資收入 - 五險一金 - 個稅起徵點) * 稅率 - 速算扣除數
其中,稅率和速算扣除數可以查下表得到:
級數 | 含稅級距 | 稅率 | 速算扣除數 |
---|---|---|---|
1 | 不超過1500元的 | 3 | 0 |
2 | 超過1500元至4500元的部分 | 10 | 105 |
3 | 超過4500元至9000元的部分 | 20 | 555 |
4 | 超過9000元至35000元的部分 | 25 | 1005 |
5 | 超過35000元至55000元的部分 | 30 | 2755 |
6 | 超過55000元至80000元的部分 | 35 | 5505 |
7 | 超過80000元的部分 | 45 | 13505 |
package com.lovoinfo;
import java.util.Scanner;
public class Tax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入工資: ¥");
double salary = sc.nextDouble();
double add = salary - salary * 0.08 - 3500;
double tax;
if(add <= 0) {
tax = 0;
}
else if(add <= 1500) {
tax = add * 0.03;
}
else if(add <= 4500) {
tax = add * 0.1 - 105;
}
else if(add <= 9000) {
tax = add * 0.2 - 555;
}
else if(add <= 35000) {
tax = add * 0.25 - 1005;
}
else if(add <= 55000) {
tax = add * 0.3 - 2755;
}
else if(add <= 80000) {
tax = add * 0.35 - 5505;
}
else {
tax = add * 0.45 - 13505;
}
System.out.printf("需要繳納的個人所得稅: ¥%.2f元\n", tax);
sc.close();
}
}
作業:輸入一個百分制的成績,將其轉換成對應的等級,規則如下表所示:
成績 | 等級 |
---|---|
90-100 | A |
80-89 | B |
60-79 | C |
<60 | D |
package day0423;
import java.util.Scanner;
public class mark {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的成績:");
int a = sc.nextInt();
if(a<100&&a>=90){
System.out.println("A");
}
else if(a<90&&a>=80){
System.out.println("B");
}
else if(a<80&&a>=60){
System.out.println("C");
}
else if(a<60){
System.out.println("D");
}
sc.close();
}
}
練習4:輸入成績等級輸出對應的評語,規則如下表所示:
成績等級 | 評語 |
---|---|
A | 該生成績優異,學習踏實認真 |
B | 該生積極上進,學習態度較好 |
C | 該生學習努力,成績有待提高 |
D | 該生成績穩定,動手能力很強 |
package day0423;
import java.util.Scanner;
public class sS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入成績等級");
char level = sc.next().charAt(0);
switch(level){
case'a':
case'A':
System.out.println("該生學習優異,學習踏實認真");
break;
case'b':
case'B':
System.out.println("該生積極上進,學習態度較好");
break;
case'c':
case'C':
System.out.println("該生學習努力,成績有待提高");
break;
case'd':
case'D':
System.out.println("該生成績穩定,動手能力很強");
break;
default:
System.out.println("逗比,輸錯了");
sc.close();
}
}
}
練習5:將一顆色子擲60000次,統計每一面出現的次數。
package com.lovoinfo;
public class ThrowDie {
public static void main(String[] args) {
int f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0, f6 = 0;
for(int i = 1; i <= 60000; i++) {
int face = (int)(Math.random() * 6 + 1);
switch(face) {
case 1: f1++; break;
case 2: f2++; break;
case 3: f3++; break;
case 4: f4++; break;
case 5: f5++; break;
case 6: f6++; break;
}
}
System.out.println("1點出現了" + f1 + "次");
System.out.println("2點出現了" + f2 + "次");
System.out.println("3點出現了" + f3 + "次");
System.out.println("4點出現了" + f4 + "次");
System.out.println("5點出現了" + f5 + "次");
System.out.println("6點出現了" + f6 + "次");
}
}
第四天:程序邏輯-2(分支和循環)
- 練習1:猜數字(人猜/機器猜)
- 練習2:人機猜拳
- 練習3:Craps賭博遊戲
- 練習4:九九表
- 練習5:百錢百雞
第五天:數組、方法和字符串
- 一維數組
- 二維數組
- 方法的定義和使用
- 方法的遞歸調用
- 字符串用法及常用方法
第六天:面向對象入門
- 基本概念
- 定義類
- 創建和使用對象
- 實例1:時鐘
- 實例2:游泳池
第七天:深入面向對象-1
- 繼承
- 多態
- 實例1:繪圖系統
- 實例2:工資結算
- 實例3:圖書打折
第八天:深入面向對象-2
- 接口
- 抽象類
- 面向對象的設計原則
- UML
- 實例:繪圖系統