[Java] 實驗5參考代碼

來源:互聯網
上載者:User

標籤:[]   模板   ctr   cond   before   orange   for   net   while迴圈   

實驗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) {// todoSystem.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();// todoSystem.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()) {// todoSystem.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;// todoSystem.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參考代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.