java键盘输入语句_java的输入语句小结

java键盘输入语句_java的输入语句小结1.使用Scanner使用时需要引入包importjava.util.Scanner;首先定义Scanner对象Scannersc=newScanner(System.in);如果要输入整数,则intn=sc.nextInt();String类型的,则Stringtemp=sc.next();比如:importjava.util.Scanner;publicclass…

大家好,又见面了,我是你们的朋友全栈君。

1.使用Scanner

使用时需要引入包import java.util.Scanner;首先定义Scanner对象

Scanner sc = new Scanner(System.in);

如果要输入整数,则 int n = sc.nextInt();

String类型的,则String temp = sc.next();

比如:

import java.util.Scanner;public class Test { public static void main(String[] args) {

Scanner scanner = new Scanner(System.in); int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int month = -1; while(true) { try {

System.out.print(“请输入月份:”);

month = scanner.nextInt(); if(month >= 1 && month <= 12) { break;

}

System.out.println(“** 请输入正确的月份 **”);

} catch (Exception e) {

System.out.println(“** 格式错误!请输入数字 **”);

scanner.next();

}

}

System.out.println(month + ” 月份有:” + days[month – 1] + ” 天”);

}

}

2.使用BufferedReader

用前需要引入 import java.io.Reader;

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

String input = br.readLine();

比如:

==================================================================================================

import java.io.*;

public class importtext {

public static void main(String[] args) {

String st;

int num;

float fnum;

try{

System.out.print(“输入:”);

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

st = br.readLine();

System.out.print(“输入一个数:”);

num = Integer.parseInt(br.readLine());

System.out.print(“输入一个浮点数:”);

fnum = Float.parseFloat(br.readLine());

System.out.print(“输出:”+st+’\n’);

System.out.print(“输出:”+num+’\n’);

System.out.print(“输出:”+fnum+’\n’);

}catch(IOException e){}

}

}

==================================================================================================

package com.s2;

import java.io.*;

public class Input

{

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

{

while(true)

{

BufferedReader  buf;

String str;

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

System.out.println(“Input a string:”);

str=buf.readLine();

System.out.println(“String=”+str);

}

}

}

==================================================================================================

应该注意的是:Java把从键盘输入的数据一律看作是字符串,因此若要从键盘输入并让系统认可是数值型数据,必须经过转换。

比如:

package com.s2;

import java.io.*;

public class Input

{

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

{

while(true)

{

int num;

BufferedReader  buf;

String str;

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

System.out.println(“Input an integer:”);

str=buf.readLine();

num=Integer.parseInt(str);

System.out.println(“String=”+str);

System.out.println(“Integer=”+str);

}

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/156395.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月7日 上午10:36
下一篇 2022年7月7日 上午10:36


相关推荐

  • 【Python 打开文件夹】——两种打开文件夹的方法

    【Python 打开文件夹】——两种打开文件夹的方法点个赞留个关注吧 第一种 importosstar directory r C Windows System32 os system explorer exe s start directory 第二种 importosstar directory r D os startfile start directory 运行后 会自动打开该文件夹

    2026年3月26日
    3
  • STM32移植LWIP

    STM32移植LWIP本文使用的是STM32F207VCT6平台,MII接口的RTL8201EL网络芯片,LWIP版本是1.4.1基础工程是:已经实现了10ms定时,led灯1s闪烁,还有串口打印欢迎查看本文所在的系列,STM32的LWIP应用,点击跳转本文使用的IDE是IAR7.2,考虑到很多很使用Keil,本文末尾也有keil版本的说明添加以太网驱动库添加进工程,增加新库的头文件路径将LWIP源码放入目录中我们把s…

    2022年7月20日
    15
  • Yourphp系统发生错误

    Yourphp系统发生错误

    2021年10月9日
    57
  • window清理系统垃圾文件代码

    window清理系统垃圾文件代码创建一个txt,后缀改为.bat,文件名自己取,但是要知道这个文件是拿来清理系统垃圾的。代码如下:@echooffpauseecho正在清理系统垃圾文件,请稍等……del/f/s/q%systemdrive%\*.tmpdel/f/s/q%systemdrive%\*._mpdel/f/s/q%systemdrive%\*.logdel/f…

    2022年6月18日
    23
  • imx8mq – bootloader编译过程

    imx8mq – bootloader编译过程一 bootloader 源码准备和编译先准备以下几个压缩包 可以独立下载压缩包 也可以通过 yocto 工程获取 l l work imx8mq sdk bootloader ll 总用量 18480drwxr xr x4ll40966 月 72018firmwar imx 7 4 rwxrwxrwx1ll 月 1009 55

    2026年3月26日
    2
  • RAID 磁盘阵列 详解[通俗易懂]

    RAID 磁盘阵列 详解[通俗易懂]RAID,Redundant Arrays of Independent Disks的简称,独立磁盘冗余阵列,简称磁盘阵列。 磁盘阵列其实也分为软阵列 (Software Raid)和硬阵列 (Hardware Raid) 两种.  软阵列:即通过软件程序并由计算机的 CPU提供运行能力所成. 由于软件程式不是一个完整系统故只能提供最基本的 RAID容错功能. 其他如热备用硬盘的设置,

    2022年6月9日
    64

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号