Java語言程序設計課程實驗題目 第二次實驗

1. 在IDE中輸入以下代碼,觀察運行結果,並說明該段程序的作用。

import java.util.Scanner;

 

public class DisplayTime {

        

         public static void main(String[] args) {

                   Scanner input = new Scanner(System.in);

                   System.out.println("Enter an integer for seconds: ");

                   int seconds = input.nextInt();

                   int minutes = seconds / 60;

                   int remainingSeconds = seconds % 60;

                   System.out.println(seconds + " seconds is " + minutes +

                                     " minutes and "+ remainingSeconds + " seconds");

         }

}

 

2. 在IDE中輸入以下代碼,觀察運行結果,並說明該段程序的作用。

import javax.swing.JOptionPane;

 

public class ComputeLoanUsingInputDialog {

         public static void main(String[] args){

                   //Enter yearly interest rate

                   String annualInterestRateString = JOptionPane.showInputDialog(

                                    "Enter yearly interest rate,for example 8.25: ");

                  

                   //Covert string to double

                   double annualInterestRate =

                            Double.parseDouble(annualInterestRateString);

                  

                   //Obtain monthly interest rate

                   double monthlyInterestRate = annualInterestRate / 1200;

                  

                   //Enter number of years

                   String numberOfYearString = JOptionPane.showInputDialog(

                                     "Enter numbers of years as an integer, \nfor example 5: ");

                  

             //Convert string to int

                   int numberOfYears = Integer.parseInt(numberOfYearString);

                  

                   //Enter loan amount

                  String loanString = JOptionPane.showInputDialog(

                                     "Enter loan amount,for example 120000.95:");

                  

                   //Convert String to double

                   double loanAmount = Double.parseDouble(loanString);

                  

                   //Calclate payment

                   double monthlyPayment = loanAmount * monthlyInterestRate / (1

                                     - 1/ Math.pow(1 + monthlyInterestRate, numberOfYears * 12));

                   double totalPayment = monthlyPayment * numberOfYears *12;

                  

                   //Format to keep two digits after the decimal point

                   monthlyPayment = (int)(monthlyPayment * 100) / 100.0;

                   totalPayment =(int)(totalPayment * 100) / 100.0;

                  

                   //Display results

                   String output = "The monthly payment is " + monthlyPayment +

                   "\nThe total payment is " + totalPayment;

                   JOptionPane.showMessageDialog(null,output);

                           

         }

 

}

 

3. 編寫程序:

讀取一個在0-99999之間的整數,將該整數的各位數字相加,如求和結果大於9,則繼續對所獲得的結果數字按各位相加求和,直至求出的和值小於10。

 

4. 編寫程序:

給定一個10位的整數組成的串,形式如: 。

其中最後的一位(即 )是校驗和,其使用以下運算規則以前面的9位上的整數作爲參數獲得結果,運算規則爲:

 ,特別來說,如果校驗和值爲10,則將校驗位的取值寫爲X。

根據如上需求,編寫程序,提示用戶輸入前9個數,然後計算得出第10位校驗和,顯示輸出經過校驗且校驗合格的完整整數串。

 

5. 編寫程序:

對輸入的字符串進行反轉並顯示結果,例如輸入字符串爲hello cust,則程序應輸出tsuc olleh,不允許使用已封裝的JDK方法進行處理。

 

1、

package org.cust.test2;

import java.util.Scanner;

public class DisplayTime {
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter an integer for seconds: ");
        int seconds = input.nextInt();
        int minutes = seconds / 60; 
        int remainingSeconds = seconds % 60; 
        System.out.println(seconds + " seconds is " + minutes +
                " minutes and "+ remainingSeconds + " seconds");
    }
}
//把輸入的秒數轉換爲多少分多少秒的形式

 

2、

package org.cust.test2;
import javax.swing.JOptionPane;

public class ComputeLoanUsingInputDialog {
    public static void main(String[] args){
        //Enter yearly interest rate
        String annualInterestRateString = JOptionPane.showInputDialog(
                "Enter yearly interest rate,for example 8.25: ");
        
        //Covert string to double
        double annualInterestRate =
            Double.parseDouble(annualInterestRateString);
        
        //Obtain monthly interest rate
        double monthlyInterestRate = annualInterestRate / 1200;
        
        //Enter number of years
        String numberOfYearString = JOptionPane.showInputDialog(
                "Enter numbers of years as an integer, \nfor example 5: ");
        
        //Convert string to int
        int numberOfYears = Integer.parseInt(numberOfYearString);
        
        //Enter loan amount
        String loanString = JOptionPane.showInputDialog(
                "Enter loan amount,for example 120000.95:");
        
        //Convert String to double
        double loanAmount = Double.parseDouble(loanString);
        
        //Calclate payment 
        double monthlyPayment = loanAmount * monthlyInterestRate / (1
                - 1/ Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
        double totalPayment = monthlyPayment * numberOfYears *12;
        
        //Format to keep two digits after the decimal point
        monthlyPayment = (int)(monthlyPayment * 100) / 100.0;
        totalPayment =(int)(totalPayment * 100) / 100.0;
        
        //Display results
        String output = "The monthly payment is " + monthlyPayment +
        "\nThe total payment is " + totalPayment;
        JOptionPane.showMessageDialog(null,output);
            
    }
    //輸入要借的本金、利率、借多少年 最後輸出每個月還多少以及總額 

}
 

3、

package org.cust.test2;

import java.util.Random;
import java.util.Scanner;

public class Test_1 {
    int result;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Test_1 t = new Test_1();
        int rNumber = 0, nChoice = 0;
        String choice, number;
        boolean flag = true, sFlag = true;
        while (flag) {
            System.out.println("請輸入選擇:" + "\n1、自動生成1-99999的數字\n2、手動輸入1-99999的數字");
            choice = in.next();
            try {
                nChoice = Integer.parseInt(choice);
            } catch (NumberFormatException e) {
                System.out.println("輸入有誤請重新輸入:");
                continue;
            }
            if (nChoice == 1) {
                Random rand = new Random();
                rNumber = rand.nextInt(100000);
                System.out.println("所取的隨機數爲:" + rNumber);
                flag = false;
            } else if (nChoice == 2) {
                while (sFlag) {
                    System.out.println("請在下面手動輸入數字");
                    number = in.next();
                    try {
                        rNumber = Integer.parseInt(number);
                        if (rNumber > 99999 || rNumber < 1) {
                            System.out.println("輸入範圍有誤");
                            continue;
                        } else {
                            sFlag = false;
                        }
                    }

                    catch (NumberFormatException e) {
                        System.out.println("輸入有誤請重新輸入:");
                        continue;
                    }

                    flag = false;
                }
            } else {
                System.out.println("輸入數字範圍有誤,請重新輸入");
                continue;
            }

        }

        t.getR(rNumber, t);
        System.out.println("處理結果爲:" + t.result);

    }

    public void getR(int r, Test_1 t) {
        int result = 0;
        int a[] = new int[6];
        int i = 0;
        do {
            a[i] = r % 10;
            r = r / 10;
            i++;
            if (r < 10) {
                a[i] = r;
                i++;
            }
        } while (r / 10 != 0);
        for (int j = 0; j < a.length; j++) {
            result += a[j];
        }
        if (result < 10) {
            t.result = result;
        } else {
            t.getR(result, t);
        }

    }
}

4、

package org.cust.test2;

import java.util.Scanner;

public class Test_2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int nArray[] = new int[9];
        String sArray[] = new String[9];
        while (true) {
            System.out.println("請選擇輸入前9位數的方式:1、依次輸入 2、一次輸入");
            int i = in.nextInt();
            if (i == 1) {
                for (int j = 0; j < nArray.length; j++) {
                    System.out.print("第" + (j + 1) + "位數:");
                    nArray[j] = in.nextInt();
                }
                String r = getResult(nArray);
                System.out.println("完整整數串爲:");
                for (int q = 0; i < nArray.length; i++) {
                    System.out.print(nArray[i]);
                }
                System.out.print(r);
                System.out.println("");
            } else if (i == 2) {
                do {
                    sArray = enter();
                } while (sArray == null);
                for (int y = 0; y < sArray.length; y++) {
                    nArray[y] = Integer.parseInt(sArray[y]);
                }
                String r = getResult(nArray);
                System.out.println("完整整數串爲:");
                for (int p = 0; p < nArray.length; p++) {
                    System.out.print(nArray[p]);
                }
                System.out.print(r);
                System.out.println("");
            } else {
                System.out.println("選擇有誤,請重新輸入");
            }
        }

    }

    public static String[] enter() {
        System.out.println("直接輸入如下:");
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        String[]eArray=input.split("");
        System.out.println(eArray.length);
        for(int i=0;i<eArray.length;i++){
            System.out.println(eArray[i]);
        }
        if (eArray.length == 9) {
            return eArray;
        } else {
            System.out.println("輸入整數串長度不爲9,請重新輸入");
            return null;
        }
    }

    public static String getResult(int[] iArray) {
        String result = null;
        int sum = 0;
        for (int z = 1; z <= iArray.length; z++) {
            sum += iArray[z - 1] * z;
        }
        sum = sum % 11;
        if (sum == 10) {
            result = "X";

        } else {
            result = sum + "";
        }
        return result;
    }
}
 

5、

package org.cust.test2;

import java.util.Scanner;

public class Test_3 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input;
        System.out.println("請輸入字符串:");
        input = in.nextLine();
        System.out.println("反轉後的字符串如下:");
        char[] cArray = input.toCharArray();
        for (int i = 1; i <= cArray.length; i++) {
            System.out.print(cArray[cArray.length - i]);
        }
    }
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章