返回列表 發帖

資料輸入 (二)

本帖最後由 鄭繼威 於 2023-4-12 20:34 編輯

運用套件 java.io 下的 Console 類別, 作整數與浮點數的輸入練習.
(console讀到的型態是字串要轉型態跟python一樣)
需搭配 parseInt() 或 parseFloat() 方法, 進行資料型態的轉換


  1. import java.io.Console;
  2. public class Ch06
  3. {
  4.      public static void main(String args[])
  5.      {
  6.            int x;
  7.            float y;
  8.            Console c=System.console();
  9.            System.out.print("輸入一整數: ");
  10.            x=Integer.parseInt(c.readLine());
  11.            System.out.println("剛輸入: "+x);
  12.            System.out.print("輸入一浮點數: ");
  13.            y=Float.parseFloat(c.readLine());
  14.            System.out.println("剛輸入: "+y);
  15.      }
  16. }
複製代碼
  1. import java.io.Console;
  2. public class Ch06
  3. {
  4.      public static void main(String args[])
  5.      {
  6.            int x;
  7.            float y;
  8.            Console c=System.console();
  9.            x=Integer.parseInt(c.readLine("輸入一整數: "));
  10.            System.out.println("剛輸入: "+x);
  11.            y=Float.parseFloat(c.readLine("輸入一浮點數: "));
  12.            System.out.println("剛輸入: "+y);
  13.      }
  14. }
複製代碼

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

返回列表