| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2733 人关注过本帖
标题:不知道Scanner中nextLine的用法?
只看楼主 加入收藏
E_xuan
Rank: 1
等 级:新手上路
帖 子:30
专家分:1
注 册:2009-9-13
结帖率:90%
收藏
已结贴  问题点数:2 回复次数:8 
不知道Scanner中nextLine的用法?
import java.util.*;

public class HandleExceptionDemo {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    boolean continueInput = true;

    do {
      try {
        System.out.print("Enter an integer: ");
        int number = scanner.nextInt();

        // Display the result
        System.out.println(
          "The number entered is " + number);

        continueInput = false;
      }
      catch (InputMismatchException ex) {
        System.out.println("Try again. (" +
          "Incorrect input: an integer is required)");
      scanner.nextLine(); //为什么去掉这个会是死循环?
      }
    } while (continueInput);
  }
}
搜索更多相关主题的帖子: 用法 nextLine Scanner 
2009-10-27 17:44
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:1 
分析:  
    条件1:        continueInput = false;
    条件2:        while (continueInput);   //请问它有可能为真吗?不是死循环是什么?

C#超级群 74862681,欢迎大家的到来!
2009-10-27 18:32
E_xuan
Rank: 1
等 级:新手上路
帖 子:30
专家分:1
注 册:2009-9-13
收藏
得分:0 
回复 2楼 gameohyes
能不能说明nextLine的作用?
2009-10-27 18:36
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:1 
也就是说,当输入的是字符时
continueInput = false;不会得到执行的.
  

C#超级群 74862681,欢迎大家的到来!
2009-10-27 18:37
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
nextLine--->能够接收空格后的内容
next------->与上反之

C#超级群 74862681,欢迎大家的到来!
2009-10-27 18:40
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
nextLine-->不管有没有接收到值都会继续
next---->没有接到,不会继续

C#超级群 74862681,欢迎大家的到来!
2009-10-27 18:44
E_xuan
Rank: 1
等 级:新手上路
帖 子:30
专家分:1
注 册:2009-9-13
收藏
得分:0 
回复 6楼 gameohyes
去掉那句nextLine,你试下程序,输入55sd,会是死循环。
2009-10-27 19:12
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
55sd是字符串     不是整型

C#超级群 74862681,欢迎大家的到来!
2009-10-27 19:27
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
给你修改了2个地方。
Scanner scanner = new Scanner(System.in);
            boolean continueInput = true;
 
            do {
              try {
                System.out.println("Enter an integer: ");
                int number = Integer.parseInt(scanner.nextLine()); //第一个地方
                // Display the result
                System.out.println(
                  "The number entered is " + number);
                 
 
                continueInput = false;
              }
              catch (Exception ex) { //第二个地方  主要是第一处。理解好就行
                System.out.println("Try again. (" +
                  "Incorrect input: an integer is required)");
//              scanner.nextLine(); //为什么去掉这个会是死循环?
              }
            } while (continueInput);

C#超级群 74862681,欢迎大家的到来!
2009-10-28 18:13
快速回复:不知道Scanner中nextLine的用法?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012540 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved