| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 784 人关注过本帖
标题:这里哪里有问题?
只看楼主 加入收藏
Eastsun
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:802
专家分:0
注 册:2006-12-14
收藏
得分:0 
书其实没错,楼主看书不认真看,肯定有这个Keyboard类的在前面某处讲了.
具体代码如下:

import java.io.*;
public class Keyboard {
private static BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
private static String buffer = \"\";
private static int p = 1; // buffer[p..] contains next input
private static String getToken() throws IOException {
while (buffer != null && (p>= buffer.length() ||
Character.isWhitespace(buffer.charAt(p)))) {
if (p>= buffer.length()) {
buffer = br.readLine();
p = 0;
}
else p++;
}
if (buffer == null) throw new IOException(\"Unexpected end of file.\");
int t = p;
p++;
while(p < buffer.length() &&
!(Character.isWhitespace(buffer.charAt(p))))
p++;
p++;
return(buffer.substring(t,p-1));
}
public static int readInt() {
// Consume and return an integer. Trailing delimiter consumed.
try { return Integer.parseInt(getToken());
} catch (Exception e) {
System.err.println(\"IO Exception in Keyboard.readInt\");
return 0;
}
}
public static boolean readBoolean() {
// Consume and return a boolean. Trailing delimiter consumed.
// Any string other than \"true\" (case ignored) is treated as false.
try { return new Boolean(getToken()).booleanValue();
} catch (Exception e) {
System.err.println(\"IO Exception in Keyboard.readBoolean\");
return false;
}
}
public static double readDouble() {
// Consume and return a double. Trailing delimiter consumed.
try {return new Double(getToken()).doubleValue();
} catch (Exception ioe) {
System.err.println(\"IO Exception in Keyboard.readDouble\");
return 0.0;
}
}
public static String readToken() {
// Consume and return a token. Trailing delimiter consumed.
// A token is a maximal sequence of non-whitespace characters.
// null returned on end of file
try {
while (buffer != null && (p>= buffer.length() ||
Character.isWhitespace(buffer.charAt(p)))) {
if (p>= buffer.length()) {
buffer = br.readLine();
p = 0;
}
else p++;
}
if (buffer == null) return null;
int t = p;
p++;
while(p < buffer.length() &&
!(Character.isWhitespace(buffer.charAt(p))))
p++;
p++;
return(buffer.substring(t,p-1));
} catch (IOException ioe) {
System.err.println(\"IO Exception in Keyboard.readToken()\");
return \"\";
}
}

public static char readChar() {
//Consume and return a character (which may be an end-of-line).
try {
if (buffer != null && p>buffer.length()) {
buffer = br.readLine();
p = 0;
}
if (buffer == null) throw new IOException(\"Unexpected end of file.\");
if (p == buffer.length()) { // supply end-of-line
p++;
return('\n');
}
else {
p++;
return buffer.charAt(p-1);
}
} catch (IOException ioe) {
System.err.println(\"IO Exception in Keyboard.readChar()\");
return (char)0;
}
}
public static char peekChar() {
// The next available character if any (which may be an end-of-line). The
// character is not consumed. If buffer is empty return null character.
if (buffer == null || p>buffer.length()) return('\000');
else if (p == buffer.length()) return('\n');
else return buffer.charAt(p);
}
public static String readString() {
// Consume and return the remainder of current line (end-of-line discarded).
// null returned on end of file
try {
if (buffer!= null && p>buffer.length()) {
buffer = br.readLine();
p = 0;
}
if (buffer == null) return null;
int t = p; p = buffer.length() + 1;
return buffer.substring(t);
} catch (IOException ioe) {
System.err.println(\"IO Exception in Keyboard.readString()\");
return \"\";
}
}
public static int available() {
// Number of characters available on this line (including end-of-line,
// which counts as one character, i.e. '\n')
if (buffer == null) return 0;
else return (buffer.length()+1-p);
}

public static boolean hasMoreTokens() {
// Are there more tokens on the current line?
if (buffer == null) return false;
int q = p;
while (q<buffer.length() && Character.isWhitespace(buffer.charAt(q))) q++;
return (q<buffer.length());
}

public static void skipLine() {
// Skip any remaining input on this line.
if (buffer != null) p = buffer.length() + 1;
}
public static void skipWhitespace() {
// Consumes input until a non-whitespace character is entered (which
// is not consumed).
try {
while (buffer != null && (p>= buffer.length() ||
Character.isWhitespace(buffer.charAt(p)))) {
if (p>= buffer.length()) {
buffer = br.readLine();
p = 0;
}
else p++;
}
} catch (IOException ioe) {
System.err.println(\"IO Exception in Keyboard.skipWhitespace()\");
}
}

public static boolean EndOfFile() { // More characters?
// This method is intended for use when keyboard is redirected to file
if (available()>0) return false;
try { buffer = br.readLine();
} catch (IOException ioe) {
System.err.println(\"IO Exception in Keyboard.readChar()\");
}
p = 0;
return (buffer == null);
}
}

楼主把上面这些代码复制到Keyboard.java文件中,与原来那个文件放在同一个文件夹下,编译就OK了.

My BlogClick Me
2007-03-02 21:48
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

呵呵,所以说看书也要看好上下文哦

建议楼主把JAVA基础的东西学好先


可惜不是你,陪我到最后
2007-03-02 21:54
yynn
Rank: 1
等 级:新手上路
帖 子:279
专家分:0
注 册:2005-11-4
收藏
得分:0 
我们自己老师编的书没有这么详细的,呵呵我会学好基础的!

2007-03-02 21:56
快速回复:这里哪里有问题?
数据加载中...
 
   



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

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