国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Java鍵盤輸入
初學(xué)者學(xué)Java時(shí),有時(shí)就要接收從鍵盤輸入。大家很容易想到C語言中的scanf()和C++中的cin().可Java中用什么呢?去網(wǎng)上一查好多人說用System.in誤導(dǎo)了好多人,包括曾經(jīng)的我,還有人說下一個插件安裝下。本人認(rèn)為,實(shí)現(xiàn)它其實(shí)最簡單的莫屬一個類,Java本身就提供的,這個類就是Scanner.先舉個例子如下:

  import java.util.Scanner;

  /**

  * 測試從鍵盤讀取用戶的輸入,關(guān)鍵類為Scanner

  * @author Administrator

  *

  */

  public class TestInput {

  public static void main(String[] args) {

  Scanner s = new Scanner(System.in);

  System.out.println("請輸入你的姓名:");

  String name = s.nextLine();

  System.out.println("請輸入你的年齡:");

  int age = s.nextInt();

  System.out.println("請輸入你的工資:");

  float salary = s.nextFloat();

  System.out.println("你的信息如下:");

  System.out.println("姓名:"+name+"\n"+"年齡:"+age+"\n"+"工資:"+salary);

  }

  }

  說明:Scanner的對象的方法nextLine()接收字符和字符串類型的輸入;nextInt()接收int類型的;nextFloat()接收float類型的,相信后面的就不用一一列舉了吧。

  方法2:

  BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

  System.out.print("Please input a number:");

  String str=reader.readLine();  //獲取字符串

  System.out.println("The number you input is:"+str);

  或

  InputStreamReader reader = new InputStreamReader(System.in);

  BufferedReader input = new BufferedReader(reader);

  System.out.print("Enter your age: ");

  String text = input.readLine();

  int age = new Integer(text).intValue();

  System.out.println("You are " + age + " years old, now,");

  int year = 2003 - age;

  System.out.println("so you were probably born in " + year);

  需要拋出io異常。

  import java.io.*;

  - @5 n0 _4 H: c( y: Hclass JavaIO_02N {' U- \9 P1 S, C2 m- R3 p& t

  public static void main(String param[])

  # u   P+ ]+ a2 L6 O! V {: B7 A6 r3 ^   [4 ~

  String nn1="";//定義一個String整型對象nn1: b5 T) n6 E9 f4 @9 p

  BufferedReader distream = new BufferedReader(new InputStreamReader(System.in));

  , p1 J   D/ B, @1 L" q6 P' a     System.out.println("鍵入一個整數(shù)");   r& m/ H2 s' Q   h" T( |+ j

  nn1=distream.readLine();//進(jìn)行輸入,并把輸入的數(shù)存入nn1中/ l, u4 y3 e5 R   \

  ) J+ D7 n$ u; z% v3 Z7 g# S* w0 l     int n1=Integer.parseInt(nn1);( z4 l# |( L/ q

  if(n1%2==0)

  . C6 b9 U$ D* F( y! K System.out.println(n1+"是一個偶數(shù)");

  3 ?! ^" c% C9 ], i- G9 L9 }/ b        else( u- G, Z( Z* {   V# _; _0 O

  System.out.println(n1+"是一個奇數(shù)"); 2 _; b) N, Q2 \4 W9 D: y$ t! E5 ~# J

  }% c! z' y( d6 d/ v# {. _7 Q

  }

  1 F: w6 M1 H) |8 j) O: e' C4 e

  & v" }5 S9 }& Y& l8 B

  " f9 G8 K, H+ A6 P% ajava不可能出現(xiàn)語句在任何方法,類之外的

  /* * Created on 2005-4-5

  * * TODO To change the template for this generated file go to

  * Window - Preferences - Java - Code Style - Code Templates

  */

  import java.io.BufferedReader;

  import java.io.IOException;

  import java.io.InputStreamReader;

  /**

  * @author Zhangql

  *

  * TODO To change the template for this generated type comment go to

  * Window - Preferences - Java - Code Style - Code Templates

  */

  public class EnterConsole

  {

  public static void main(String[] args) throws IOException

  {

  EnterConsole enterConsole = new EnterConsole();

  enterConsole.printConsoleChar();

  }

  /**

  * 從控制對接收一行字符串,然后輸出到控制臺

  * @throws IOException

  */

  public void printConsoleLine() throws IOException

  {

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  String str = null;

  System.out.println("Enter your value:");

  str = br.readLine();

  System.out.println("your value is :"+str);

  }

  /**

  * 從控制臺接收一個字符

  * 然后打印到控制臺上

  * @throws IOException

  */

  public void printConsoleChar() throws IOException

  {

  System.out.print("Enter a Char:");

  char i = (char) System.in.read();

  System.out.println("your char is :"+i);

  }

  }

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
在java中如何用鍵盤輸入一個數(shù),字符,字符串
java之socket的OOBInline和UrgentData和發(fā)送心跳包研究
Socket 實(shí)現(xiàn)同一網(wǎng)絡(luò)下的實(shí)時(shí)通信
一步一步android(15):關(guān)于socket編程【以聊天為例】_目睹一個Geek的生活...
淺出Java Socket 編程
java程序獲得windows系統(tǒng)的一些參數(shù)(cmd指令運(yùn)行返回結(jié)果)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服