黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 一文讀懂Java怎么用鍵盤輸入

一文讀懂Java怎么用鍵盤輸入

更新時間:2022-04-06 11:13:23 來源:動力節點 瀏覽1542次

動力節點小編告訴大家,在Java中接受鍵盤輸入是使用 Scanner 對象完成的。

考慮以下陳述

Scanner console = new Scanner(System.in)

該語句聲明了一個名為 console 的引用變量。Scanner 對象與標準輸入設備 (System.in) 相關聯。

要從鍵盤獲取輸入,您可以調用 Scanner 類的方法。例如在以下語句中 Scanner 的 nextInt() 方法接受一個整數并返回變量 x :

int x = console.nextInt();

例子 :

import java.util.Scanner;    // Needed for Scanner class
/**
 *  This program demonstrates keyboard input.
 */
public class RectangleArea
{
   public static void main(String[] args)
   {
      int length;    // To hold rectangle's length.
      int width;     // To hold rectangle's width.
      int area;      // To hold rectangle's area
      // Create a Scanner object to read input.
      Scanner console = new Scanner(System.in);
      // Get length from the user.
      System.out.print("Enter length ");
      length = console.nextInt();
      // Get width from the user.
      System.out.print("Enter width ");
      width = console.nextInt();
      // Calculate area.
      area = length * width;
      // Display area.
      System.out.println("The area of rectangle is " + area);
   }
}

輸出 :

輸入長度5
輸入寬度8
矩形的面積是40

Scanner 類的其他一些有用的方法

例子 :

import java.util.Scanner;
/**
 * This program demonstrates various Scanner methods.
 */
public class ReadEmployee
{
    public static void main(String[] args)
    {
        String name; // To hold the employee's name
        int age; // To hold the employee's age
        char gender; // To hold the employee's gender
        double salary; // To hold the employee's salary
        // Create a Scanner object to read input.
        Scanner console = new Scanner(System.in);
        // Get the employee's name
        System.out.print("Enter name: ");
        name = console.nextLine();
        // Get the employee's age
        System.out.print("Enter age: ");
        age = console.nextInt();
        // Get the employee's gender
        System.out.print("Enter gender: ");
        gender = console.next().charAt(0);
        // Get the employee's salary
        System.out.print("Enter salary: ");
        salary = console.nextDouble();
        // Display the information
        System.out.println("Name: " + name + " Age: " + age + " Gender: "
                + gender + " Salary: " + salary);
    }
}

輸出 :

輸入姓名:Alex Joseph
輸入年齡:24
輸入性別:M
輸入薪水:8000
姓名:Alex Joseph 年齡:24 性別:M 薪水:8000.0

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 一二三区在线观看 | 一个人看的免费视频www免费 | 一个人在线免费观看www视频 | 亚洲欧美成人网 | 黄色网址免费在线 | 免费黄色在线网站 | 免费黄色毛片视频 | 免费a网址 | 国产一级片毛片 | 国产精品成人观看视频国产 | 国产黄色片在线观看 | 最近的中文字幕免费视频1 最近的中文字幕大全免费版 | 看黄视频网站 | 成人免费毛片观看 | 日本理论片在线播放 | 成年人黄色小视频 | 欧美乱淫 | 欧美 另类 精品一区视频 | 午夜夜伦鲁鲁片 | 99视频精品全国在线观看 | 亚洲综合插 | 欧美18—19sex性护士 | 久久精品五月天 | 日本福利写真福利视频 | 欧美xxx性 | 最近免费中文字幕高清大全 | 国产精品亚洲一区二区三区正片 | 欧美日韩国产在线成人网 | 天天干网 | 全网免费在线播放视频入口 | 日韩伦理片在线 | 日韩视频在线观看视频 | 91短视频黄 | 日韩毛片高清免费 | 欧美精品束缚一区二区三区 | 国产高清一区二区三区视频 | 国产精品日本一区二区在线看 | 中文一区二区在线观看 | 美女私密福利第一导航 | 成年在线观看网站免费视频 | 日本久久一区二区 |