java scan.next_Java使用scanner.nextLine()
尝试使用java.util.Scanner中的nextLine()方法时遇到麻烦。
这是我尝试过的:
import java.util.Scanner;
class TestRevised {
public void menu() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence:\t");
String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
int index = scanner.nextInt();
System.out.println("\nYour sentence:\t" + sentence);
System.out.println("Your index:\t" + index);
}
}
示例1:此示例按预期方式工作。该行String sentence = scanner.nextLine();等待输入,然后再继续System.out.print("Enter an index:\t");。
产生输出:
Enter a sentence: Hello.
Enter an index: 0
Your sentence: Hello.
Your index: 0
// Example #2
import java.util.Scanner;
class Test {
public void menu() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nMenu Options\n");
System.out.println("(1) - do this");
System.out.println("(2) - quit");
System.out.print("Please enter your selection:\t");
int selection = scanner.nextInt();
if (selection == 1) {
System.out.print("Enter a sentence:\t");
String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
int index = scanner.nextInt();
System.out.println("\nYour sentence:\t" + sentence);
System.out.println("Your index:\t" + index);
}
else if (selection == 2) {
break;
}
}
}
}
示例2:此示例无法正常工作。本示例使用while循环以及if-else结构允许用户选择要执行的操作。一旦程序到达String sentence = scanner.nextLine();,它就不会等待输入,而是执行该行System.out.print("Enter an index:\t");。
产生输出:
Menu Options
(1) - do this
(2) - quit
Please enter your selection: 1
Enter a sentence: Enter an index:
这使得不可能输入句子。
为什么示例2不能按预期工作?唯一之间的区别。1和2是那个 2具有while循环和if-else结构。我不明白为什么这会影响Scanner.nextInt()的行为。
java scan.next_Java使用scanner.nextLine()相关推荐
- 【尚硅谷-Java学习】scanner.next()和scanner.nextLine()
scanner的使用 使用Scanner可以获取键盘的输入. //首先导入Scanner包 import java.util.Scanner; public class Main{public sta ...
- java控制台next_java读取控制台输入以及next() ,nextLine(), nextInt() 和nextDouble()的比较...
(1)读取控制台输入 java的控制台输入由System.in完成. 为了得到一个绑定到控制台的字符流,你能够把System.in包装在一个BufferedReader对象中建立一个字符流.html ...
- Java Scanner.nextLine()读取回车问题解决
Scanner.nextLine读取回车问题解决 问题描述 问题分析 如何处理多出来的换行符 方法一 方法二 实战 问题解答 问题描述 我们在使用java读取键盘输入时,如果先读取一个int变量,再读 ...
- java使用scanner.nextline方式接收键盘输入
import java.util.Scanner; //nextline 方式接收 public class Demo02 {public static void main(String[] args ...
- scanner.nextLine()和scanner.next();的区别
scanner.nextLine() 和 scanner.next(); 的区别 查看 scanner.nextLine() 的源码,可以发现是一次性读完的 /*** Advances this sc ...
- java中输入方式Scanner和BufferedReader
Scanner 在java.util包中,需要引入时可以用import java.util.*;或者import java.util.Scanner; 工作原理:通过new创建一个Scanner对象, ...
- Java十六:Scanner,配合hasNext()/hasNextInt()/hasNextFloat()....实现人机互动
Scanner:配合hasNext()/hasNextInt()/hasNextFloat()-实现人机互动 package com.kuangstudy.ProcessControl;import ...
- 关于Scanner.nextLine()问题的解决方案
@TOC 刚学习java,我在做练习码Demo的时候总是碰见关于Scanner.nextLine空字符的问题,导致在运行程序时无法在控制台获得录入值,而是直接接收了一个空字符.对于我这种懒人,每次产生 ...
- idea 中运行 Scanner.nextLine() 一直卡住
某日, 在 Junit 下, 想读取用户输入, 于是编写以下代码, 发现一直卡住, 无法接收用户输入 @Testpublic void testScanner() {Scanner scanner = ...
最新文章
- 过滤内容字段_巧用参数组件和过滤组件,教你快速定位目标数据
- l2_norm opencv torch比较
- C#调用C++ DLL的方式
- maven项目配置定时任务
- 关于realarm S5P4418的u-boot显示内存大小问题解决方法
- shell 调用 sqlplus 各种情况示例
- java多线程教程_java 基础教程之多线程详解及简单实例
- 邻近算法(KNN算法)
- android 左右卡片切换,3D卡片切换
- JSCore的基本使用
- Spring Cloud版——电影售票系统七使用 Zuul 构建微服务网关
- php开源个人网站,PHP开源软件《个人管理系统》
- VL1_四选一多路器(完整RTL、Testbench和覆盖率)
- 平衡二叉树(AVL树)深入解读
- 房产销售数据分析与可视化的设计与实现
- php kb转换成字节,将kB换算为MB (千字节换算为兆字节)
- PDF Expert for mac(最好用的pdf编辑工具)
- 令人敬畏的泰格伍兹 万维钢_5个令人敬畏的全新高级jQuery插件,2013年10月
- 天猫order前后台
- 国人劝酒经典用语大全
热门文章
- linux服务器重启原因排查_Linux服务器不明原因重启了,怎么查找原因
- 曝光修正相关工作:Related Work on the Exposure Correction
- 八通道IEPE振动传感器数据高速同步网络采集模块 WJ288
- 计算机毕业设计JAVA唯艺健身管理系统演示录像mybatis+源码+调试部署+系统+数据库+lw
- 阿里大佬倾情力荐:Java全线成长宝典,从P5到P8一应俱全
- DataDig 5.0.0::自动提取论文图表中曲线数据的软件
- Angular中优雅的处理RxJs自动取消订阅的方式以免出现内存泄露以及多次调用的问题
- js和jquery验证密码必须为字母加数字
- Aspect @Pointcut切入点解释
- 安卓端微信H5下载文件处理:让微信自动弹起跳转外部浏览器窗口