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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • tracker服务器地址(每日更新)_hypixel服务器地址

    tracker服务器地址(每日更新)_hypixel服务器地址https://dns.icoa.cn/tracker/udp://tracker.tiny-vps.com:6969/announcehttps://1337.abcvg.info/announcehttps://tracker.fastdownload.xyz:443/announcehttps://opentracker.xyz:443/announceh…

    2022年10月1日
  • ABAP WDA

    ABAP WDA一、20181217-20181226笔记selection_options和alv 二、相关服务1、事务码:SICF默认SERVICE,执行。Service:default_host/sap/option/*default_host/sap/public/bc/*default_host/sap/bc/wdvddefault_host/sap/bc/webdynp…

    2022年7月12日
  • 关于净推荐值(NPS)的理解

    关于净推荐值(NPS)的理解NPS在产品领域用来衡量用户对产品体验的评价情况,同时也可以用在某个员工在公司受到大家的评价情况,类似等等。本文将主要关注在产品领域。如何衡量你的用户对产品的使用满意度呢?NPS就是一个非常重要的标准。那么,如何搭建NPS模型,利用NPS指标更加直观、具体地完成用户调研呢?以下,笔者将详细为大家讲述。现在几乎所有的互联网公司,都会说:“以用户为中心”,那到底是什么以用户为中心?…

    2022年4月28日
  • Mysql ID生成器

    Mysql ID生成器Mysql可以作为分布式序列号生成器,写下笔记以防忘记。需要一张表server_id_table表中的role为服务器角色名,nextId为当前Id,startId为开始Id,endId为结束Id。使用下列sql语句可以实现分布式Id生成器的功能,而且是线程安全的

    2022年6月16日
  • ios的邮箱怎么用_ios邮箱怎么添加多个账号

    ios的邮箱怎么用_ios邮箱怎么添加多个账号//该方法在不退出应用程序的前提下调用短信和邮箱,以下内容请在真机测试//导入框架MessageUI.framework#import”ViewController.h”//首先导入头文件#import#import//代理@interfaceViewController()MFMailComposeVi

    2022年10月13日
  • Simple Automated Backups for MongoDB Replica Sets

    Simple Automated Backups for MongoDB Replica Sets

    2022年1月2日

发表回复

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

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