Poechant 練習 Java API – 封裝類

來源:互聯網
上載者:User

利用三個封裝類編寫一個計算三個整數平均值的程式。程式首先提示使用者輸入3個整數,然後將3個整數分別賦予3個Integer對象。利用適當的封裝類方法將3個整數轉換成整數值。將其平均值封裝到一個Double對象。最後列印這三個整數值以及平均值。程式將繼續運行直到使用者打入“n”。

import java.util.Scanner;public class test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);Integer in[] = new Integer[3];int count = 0;boolean isValid = false;while(!isValid){if (sc.hasNextInt()) {String str = sc.next();in[count++] = Integer.parseInt(str);if (count == 3) {Double dou = new Double((in[0].intValue() + in[1].intValue() + in[2].intValue()) / 3.0);System.out.print(in[0].intValue() + "\t" + in[1].intValue() + "\t" + in[2].intValue());System.out.println("\t" + dou.doubleValue());sc.nextLine();count = 0;}} else if (sc.next().equals("n")) {isValid = true;} else {System.out.println("Please input an integer or \"N\".");sc.nextLine();}}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.