[Java] 实验5參考代码
實驗4月3日晚截止,實驗截止后將在此給出完整的參考代碼。
1. 怎樣使用以下的代碼模板:
1.1 在eclipse中創建相應名稱的類
1.2 將代碼拷貝到類文件中
1.3 在//todo凝視中輸入你用于解題的代碼。
1.4 樣例:參考第一題“顯示兩級名字”。大家就能夠這么做
1.4.1 在eclipse中創建類。名字叫做PassOrFail
1.4.2 將以下的代碼拷貝到.java文件里。并刪除//todo凝視,開始在while循環里寫代碼 (推斷成績是否大于60, 輸出等)
1.4.3 將寫好的PassOrFail.java上交到實驗站點上
2. 不了解什么是縮進的能夠參考什么是代碼縮進(code indent), 或與周圍同學討論。
3. 自己主動排版快捷鍵:ctrl + shift + F (非常方便的,一定要試試。誰用誰知道:P)
顯示兩級名字
這題目的名字真爛... 代碼:
import java.util.Scanner;
public class PassOrFail {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
// todo
}
}
}
找最小值
提示
1. 兩個數的最小值
int minimal = Math.min(a, b);
import java.util.Scanner;
public class FindMinimal {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
// todo
System.out.println("min is " + min);
}
}
}
求三角形的面積和周長
提示
1. if后面跟多條語句的時候,須要用花括號,將“多條語句”括起來,使它們變成一個“復合語句”.
2. 條件A, B的“邏輯與”關系
if (conditionA && conditionB) {
// todo
} else {
// todo
}
3. 條件A, B的“邏輯或”關系
if (conditionA || conditionB) {
// todo
} else {
// todo
}
4. 求平方根
sqrt方法大家之前用過,能夠去看[Java] 實驗3參考代碼.
代碼模板
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
float a = in.nextFloat();
float b = in.nextFloat();
float c = in.nextFloat();
// todo
}
}
}
推斷數的符號
import java.util.Scanner;
public class JudgeSign {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
int num = in.nextInt();
// todo
}
}
}
計算個人所得稅
提示
1. 什么是初始化
大家能夠去群里下載《Java核心技術 卷1 基礎知識 (原書第9版)》,參考"3.4.1 變量初始化"部分。
2. 為什么在這道題中,有些同學會出現編譯錯誤:“可能使用未初始化的變量”
依據語法,變量在讀取前必須被賦值(Variables must be initialized before used.).
考慮下述代碼:
double rate;
if (conditionA) {
rete = 0.05;
} else if (condition B) {
rate = 0.1;
} else if (condition C) {
rate = 0.2;
}
double res = rate; // compilation error!!!
代碼最后一行做的事。是讀取rate的值,將這個值賦值給res.
依據語法,這個rate的值在被讀取前須要被初始化(非正式地能夠理解成“賦值”)。問題是,從編譯器的角度上看,假設分支A, B, C都沒有被運行,那么rate可能就沒有被賦值。所以在double res = rate中。試圖讀取rate的值就是非法的。
有些同學會說,為解決問題,能夠在定義rate的時候先隨便給它賦一個值:double rate = 0.
這樣能夠阻止編譯器報錯,但未必是并不是最優的解決方式。假設A, B, C三個分支能包括整個條件空間。那么我覺得代碼應該寫成這樣更為合理:
double rate;
if (conditionA) {
rete = 0.05;
} else if (condition B) {
rate = 0.1;
} else { // there is no if here!!!
rate = 0.2;
}
double res = rate;
程序模板
import java.util.Scanner;
public class Tax {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
float salary = in.nextFloat();
// todo
System.out.println("tax=" + (int)(tax * 100 + 0.5) / 100d);
}
}
}
顯示水果的價格
import java.util.Scanner;
public class Fruits {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
System.out.println("[1] apples");
System.out.println("[2] pears");
System.out.println("[3] oranges");
System.out.println("[4] grapes");
int choice = in.nextInt();
// todo
}
}
}
字母轉換
import java.io.*;
public class CharacterConversion {
public static void main(String[] args) throws IOException {
for (int ch = System.in.read(); ch != '?
'; ch = System.in.read()) {
// todo
System.out.print((char)ch);
}
}
}
計算分段函數的值
import java.util.Scanner;
public class PiecewiseFunction {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
int x = in.nextInt();
double y;
// todo
System.out.println("f(" + x + ")=" + y);
}
}
}
求一元二次方程的根
提示
1. 為什么 (int) (x * 100 + 0.5) / 100d的方法有些時候會出錯?
由于這種方法僅僅能應付x >= 0的情況,考慮 x = -2.5, 那么
x * 100 = -250
-250 + 0.5 = -249.5
(int) -249.5 = -249
-249 / 100d = -2.49
注意到。在此我們想要的結果是-2.5而不是-2.49
2. 四舍五入保留兩位小數
public class Test {
public static void main(String[] args) {
double num = 3.1415926535;
for (int i = 0; i <= 10; ++ i) {
System.out.println(remainDigits(num, i));
}
}
static double remainDigits(double num, int digitsToRemain) {
// e.g. remainDigits(3.14159, 2) was called, then it will return
// Math.round(3.14159 * 100d) / 100d, which equals 3.14
return Math.round(num * Math.pow(10, digitsToRemain))
/ Math.pow(10, digitsToRemain);
}
}
import java.util.Scanner;
public class Root {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
// todo
}
}
}
顯示五級記分制成績所相應的百分制成績區間
import java.util.Scanner;
public class Grade {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat-- != 0) {
// todo
}
}
}
總結
以上是生活随笔為你收集整理的[Java] 实验5參考代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET Exceptionless 本
- 下一篇: 在线问诊 Python、FastAPI、