爱问知识人 爱问教育 医院库

Java键盘输入要注意哪些事情呢?

首页

Java键盘输入要注意哪些事情呢?

Java键盘输入要注意哪些事情呢?

提交回答

全部答案

    2018-03-29 09:06:24
  •   初学者学Java时,有时就要接收从键盘输入。大家很容易想到C语言中的scanf()和C++中的cin()。可Java中用什么呢?去网上一查好多人说用System。in误导了好多人,包括曾经的我,还有人说下一个插件安装下。本人认为,实现它其实最简单的莫属一个类,Java本身就提供的,这个类就是Scanner。
      先举个例子如下: import java。util。Scanner; /** * 测试从键盘读取用户的输入,关键类为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("键入一个整数"); r& m/ H2 s' Q h" T( |+ j nn1=distream。readLine();//进行输入,并把输入的数存入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+"是一个偶数"); 3 ?! ^" c% C9 ], i- G9 L9 }/ b else( u- G, Z( Z* { V# _; _0 O System。
      out。println(n1+"是一个奇数"); 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不可能出现语句在任何方法,类之外的 /* * 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); } } 编辑特别推荐: Java中hashmap和hashtable的区别。
      

    只***

    2018-03-29 09:06:24

类似问题

换一换
  • 系统软件 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):