日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

[Java] 实验5參考代码

發(fā)布時(shí)間:2023/10/11 115 老码农
生活随笔 收集整理的這篇文章主要介紹了 [Java] 实验5參考代码 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

實(shí)驗(yàn)4月3日晚截止,實(shí)驗(yàn)截止后將在此給出完整的參考代碼。

1. 怎樣使用以下的代碼模板:

1.1 在eclipse中創(chuàng)建相應(yīng)名稱(chēng)的類(lèi)

    1.2 將代碼拷貝到類(lèi)文件中

1.3 在//todo凝視中輸入你用于解題的代碼。

1.4 樣例:參考第一題“顯示兩級(jí)名字”。大家就能夠這么做

1.4.1 在eclipse中創(chuàng)建類(lèi)。名字叫做PassOrFail

1.4.2 將以下的代碼拷貝到.java文件里。并刪除//todo凝視,開(kāi)始在while循環(huán)里寫(xiě)代碼 (推斷成績(jī)是否大于60, 輸出等)

1.4.3 將寫(xiě)好的PassOrFail.java上交到實(shí)驗(yàn)站點(diǎn)上

2. 不了解什么是縮進(jìn)的能夠參考什么是代碼縮進(jìn)(code indent), 或與周?chē)瑢W(xué)討論。

3. 自己主動(dòng)排版快捷鍵:ctrl + shift + F (非常方便的,一定要試試。誰(shuí)用誰(shuí)知道:P)

顯示兩級(jí)名字

這題目的名字真爛... 代碼:

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. 兩個(gè)數(shù)的最小值

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);
}
}
}

求三角形的面積和周長(zhǎng)

提示

1. if后面跟多條語(yǔ)句的時(shí)候,須要用花括號(hào),將“多條語(yǔ)句”括起來(lái),使它們變成一個(gè)“復(fù)合語(yǔ)句”.

2. 條件A, B的“邏輯與”關(guān)系

if (conditionA && conditionB) {
// todo
} else {
// todo
}

3. 條件A, B的“邏輯或”關(guān)系

if (conditionA || conditionB) {
// todo
} else {
// todo
}

4. 求平方根

sqrt方法大家之前用過(guò),能夠去看[Java] 實(shí)驗(yàn)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
}
}
}

推斷數(shù)的符號(hào)

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
}
}
}

計(jì)算個(gè)人所得稅

提示

1. 什么是初始化

大家能夠去群里下載《Java核心技術(shù) 卷1 基礎(chǔ)知識(shí) (原書(shū)第9版)》,參考"3.4.1 變量初始化"部分。

2. 為什么在這道題中,有些同學(xué)會(huì)出現(xiàn)編譯錯(cuò)誤:“可能使用未初始化的變量”

依據(jù)語(yǔ)法,變量在讀取前必須被賦值(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的值,將這個(gè)值賦值給res.

依據(jù)語(yǔ)法,這個(gè)rate的值在被讀取前須要被初始化(非正式地能夠理解成“賦值”)。問(wèn)題是,從編譯器的角度上看,假設(shè)分支A, B, C都沒(méi)有被運(yùn)行,那么rate可能就沒(méi)有被賦值。所以在double res = rate中。試圖讀取rate的值就是非法的。

有些同學(xué)會(huì)說(shuō),為解決問(wèn)題,能夠在定義rate的時(shí)候先隨便給它賦一個(gè)值:double rate = 0.

這樣能夠阻止編譯器報(bào)錯(cuò),但未必是并不是最優(yōu)的解決方式。假設(shè)A, B, C三個(gè)分支能包括整個(gè)條件空間。那么我覺(jué)得代碼應(yīng)該寫(xiě)成這樣更為合理:

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);
}
}
}

顯示水果的價(jià)格

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
}
}
}

字母轉(zhuǎn)換

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);
}
}
}

計(jì)算分段函數(shù)的值

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的方法有些時(shí)候會(huì)出錯(cuò)?

由于這種方法僅僅能應(yīng)付x >= 0的情況,考慮 x = -2.5, 那么

x * 100 = -250

-250 + 0.5 = -249.5

(int) -249.5 = -249

-249 / 100d = -2.49

注意到。在此我們想要的結(jié)果是-2.5而不是-2.49

2. 四舍五入保留兩位小數(shù)

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
}
}
}

顯示五級(jí)記分制成績(jī)所相應(yīng)的百分制成績(jī)區(qū)間

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
}
}
}

總結(jié)

以上是生活随笔為你收集整理的[Java] 实验5參考代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。