当前位置:编程学习 > JAVA >>

输入int 数据时为什么要调用getString()

下面代码是从书上抄的

import java.io.*;

class TriangleApp
{
static int theNumber;

public static void main (String []args) throws IOException
{
System.out.println("Enter a number :");
theNumber = getInt();
int theAnswer = triangle(theNumber);
System.out.println("triangle =" + theAnswer);
}

public static int triangle(int n)
{
if(n ==1)
{
return 1;
}
else
{
return (n + triangle(n-1));
}
}

public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}

public static int getInt() throws IOException
{
String s = getString();
return Integer.parseInt(s);
}

}


此代码运行正确

Question:
为什么getInt()要调用getString()? 能不能直接调用getInt(),
Scanner scanner = new Scanner(System.in);
再调用 nextInt(), 从而得到 Int theNumber。

谢谢 --------------------编程问答-------------------- readLine
public String readLine() throws IOException
读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。 

返回:
包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 

这段API不知是否是楼主想要的。 --------------------编程问答-------------------- 因为你
System.out.println("Enter a number :");
输入的是一个字符串 --------------------编程问答-------------------- 你从键盘输入的数字,计算机如何判断应该为int型还是String型? --------------------编程问答-------------------- 说句实话,我不明白楼主说的是什么意思。能把描述的逻辑写得完整和清晰一些么 --------------------编程问答--------------------
Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        System.out.println(i);
--------------------编程问答-------------------- 对于我上面给的代码,你输入字符看看有什么结果! --------------------编程问答-------------------- 用Scanner类的方法netxtInt完全可以, --------------------编程问答-------------------- 晕,这代码写的。。读输入流,弄成string,再Integer.parseInt(s)。。。
完全没必要。就算是这样如果输入的字符串不是数字也会抛异常。
用nextInt吧 --------------------编程问答--------------------
引用 3 楼 AA5279AA 的回复:
你从键盘输入的数字,计算机如何判断应该为int型还是String型?


调用nextInt()能不能判断输入是否是int型呢?
我对这块只是不清楚,请不要见笑 --------------------编程问答-------------------- 这么说吧,到底是使用 Scanner 的 nextInt 或 nextLine 取决你想怎么用
nextLine 会以回车换行为界读取一行,即你输入一段文字然后回车,nextLine 便会返回你输入的字符串
而 nextInt 是以空字符(空格、换行、制表符等)为界
比如你输入"12"然后回车就会读入"12",但如果你输入的是"12 34 56 78"然后回车,这时如果你使用的是 nextLine,则读取到的字符串便是"12 34 56 78",而使用 nextInt 则只会读取到一个数字"12",再次调用则读取到下一个数字"34",以此类推知道读取到所有数字。

自己可以写段代码试试

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int m;
while (true) {
m = in.nextInt();
System.out.println(m);
System.out.println("-----------------");
}

}
--------------------编程问答-------------------- 你说的完全可以。只不过原程序加入了IO的知识,你可以好好学习了解一下。 --------------------编程问答--------------------
引用 10 楼 gd920129 的回复:
这么说吧,到底是使用 Scanner 的 nextInt 或 nextLine 取决你想怎么用
nextLine 会以回车换行为界读取一行,即你输入一段文字然后回车,nextLine 便会返回你输入的字符串
而 nextInt 是以空字符(空格、换行、制表符等)为界
比如你输入"12"然后回车就会读入"12",但如果你输入的是"12 34 56 78"然后回车,这时如果你使用的是 nextLine,则读取到的字符串便是"12 34 56 78",而使用 nextInt 则只会读取到一个数字"12",再次调用则读取到下一个数字"34",以此类推知道读取到所有数字。

自己可以写段代码试试

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int m;
while (true) {
m = in.nextInt();
System.out.println(m);
System.out.println("-----------------");
}

}


引用 10 楼 gd920129 的回复:
这么说吧,到底是使用 Scanner 的 nextInt 或 nextLine 取决你想怎么用
nextLine 会以回车换行为界读取一行,即你输入一段文字然后回车,nextLine 便会返回你输入的字符串
而 nextInt 是以空字符(空格、换行、制表符等)为界
比如你输入"12"然后回车就会读入"12",但如果你输入的是"12 34 56 78"然后回车,这时如果你使用的是 nextLine,则读取到的字符串便是"12 34 56 78",而使用 nextInt 则只会读取到一个数字"12",再次调用则读取到下一个数字"34",以此类推知道读取到所有数字。

自己可以写段代码试试

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int m;
while (true) {
m = in.nextInt();
System.out.println(m);
System.out.println("-----------------");
}

}



非常感谢你的答案,我弄清楚了,这是我改进的代码

import java.io.*;
import java.util.*;

class TriangleApp
{
static int theNumber;

public static void main (String []args) throws IOException
{
System.out.println("Enter a number :");
theNumber = getInt();
int theAnswer = triangle(theNumber);
System.out.println("triangle =" + theAnswer);
}

public static int triangle(int n)
{
if(n ==1)
{
return 1;
}
else
{
return (n + triangle(n-1));
}
}

/*
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
*/

public static int getInt() throws IOException
{
/*{
String s = getString();
return Integer.parseInt(s);
}
*/

Scanner in = new Scanner(System.in);
int m;
while (true)
{
m = in.nextInt();
System.out.println("--------------");
return m;
}

}
}


还想接着你说的 nextInt()继续问个问题
如果我输入的 “12 34”, nextInt()会读取12, 如果想再次调用读取34,如何实现。谢谢 --------------------编程问答--------------------
引用 8 楼 huxiweng 的回复:
晕,这代码写的。。读输入流,弄成string,再Integer.parseInt(s)。。。
完全没必要。就算是这样如果输入的字符串不是数字也会抛异常。
用nextInt吧

完全同意 --------------------编程问答-------------------- 用nextInt完全可以的,解决问题的思路不同罢了 --------------------编程问答--------------------
引用 12 楼 Sally_simple 的回复:
还想接着你说的 nextInt()继续问个问题
如果我输入的 “12 34”, nextInt()会读取12, 如果想再次调用读取34,如何实现。谢谢

这么说吧,我先和你解释一个 nextInt 这个方法。
nextInt 是一个阻塞方法,只有在有输入的情况下才会返回读取到的值,否则程序在执行到 nextInt 时会阻塞
而输入流是有缓冲的,比如你输入"12 34",没有回车时输入流并没有接收到你的输入,而当你按下回车,输入流便接收到你之前输入的"12 34",这时 nextInt 发现输入流中有数据,便会读取一个int值,这个值便是12,这时输入流的缓冲区里还有没读取到的"34",当你再次调用 nextInt 时便会再从缓冲区中获得34,如果接着调用 nextInt,由于输入流缓冲区中没有数据,nextInt 便会使程序再次进入阻塞状态,直到有数据进入输入流。

看完这些你应该解决了你的问题了吧
你输入"12 34"回车,"12 34"进入输入流缓冲区,调用 nextInt 返回12,再调用 nextInt 返回34,如果接下来还调用,输入流缓冲区没数据了,nextInt 便阻塞,直到有数据进入输入流。 --------------------编程问答-------------------- 除 --------------------编程问答--------------------
引用 15 楼 gd920129 的回复:
Quote: 引用 12 楼 Sally_simple 的回复:

还想接着你说的 nextInt()继续问个问题
如果我输入的 “12 34”, nextInt()会读取12, 如果想再次调用读取34,如何实现。谢谢

这么说吧,我先和你解释一个 nextInt 这个方法。
nextInt 是一个阻塞方法,只有在有输入的情况下才会返回读取到的值,否则程序在执行到 nextInt 时会阻塞
而输入流是有缓冲的,比如你输入"12 34",没有回车时输入流并没有接收到你的输入,而当你按下回车,输入流便接收到你之前输入的"12 34",这时 nextInt 发现输入流中有数据,便会读取一个int值,这个值便是12,这时输入流的缓冲区里还有没读取到的"34",当你再次调用 nextInt 时便会再从缓冲区中获得34,如果接着调用 nextInt,由于输入流缓冲区中没有数据,nextInt 便会使程序再次进入阻塞状态,直到有数据进入输入流。

看完这些你应该解决了你的问题了吧
你输入"12 34"回车,"12 34"进入输入流缓冲区,调用 nextInt 返回12,再调用 nextInt 返回34,如果接下来还调用,输入流缓冲区没数据了,nextInt 便阻塞,直到有数据进入输入流。


您讲的真详细。搞明白了,谢谢
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,