在用System.in.read()的时候,怎么判断输入的结尾.
说是到了结尾,值为-1,为什么判断不出来.
以下是一本书上的例程,但执行结果并不如预期那样.import java.io.*;
public class In
{ private static int c;
private static boolean blank()
{ return Character.isWhitespace((char) c); }
private static void readC()
{
try
{ c = System.in.read(); } //本应读到结尾使c=-1,empty()为真
catch (IOException e)
{ c = -1; }
}
public static void init()
{ readC(); }
public static boolean empty()
{ return c == -1; }
public static String getString()
{
if (empty()) return null;
String s = "";
do
{ s += (char) c; readC(); }
while (!(empty() || blank()));
while (!empty() && blank()) readC();
return s;
}
public static int getInt()
{ return Integer.parseInt(getString()); }
public static double getDouble()
{ return Double.parseDouble(getString()); }
}
说是到了结尾,值为-1,为什么判断不出来.
以下是一本书上的例程,但执行结果并不如预期那样.import java.io.*;
public class In
{ private static int c;
private static boolean blank()
{ return Character.isWhitespace((char) c); }
private static void readC()
{
try
{ c = System.in.read(); } //本应读到结尾使c=-1,empty()为真
catch (IOException e)
{ c = -1; }
}
public static void init()
{ readC(); }
public static boolean empty()
{ return c == -1; }
public static String getString()
{
if (empty()) return null;
String s = "";
do
{ s += (char) c; readC(); }
while (!(empty() || blank()));
while (!empty() && blank()) readC();
return s;
}
public static int getInt()
{ return Integer.parseInt(getString()); }
public static double getDouble()
{ return Double.parseDouble(getString()); }
}
{ public static void main(String[] args)
{ int N = Integer.parseInt(args[0]);
int id[] = new int[N];
for (int i = 0; i < N ; i++) id[i] = i;
for( In.init(); !In.empty(); )
{ int p = In.getInt(), q = In.getInt();
int t = id[p];
if (t == id[q]) continue;
for (int i = 0;i<N;i++)
if (id[i] == t) id[i] = id[q];
Out.println(" " +p+""+q);
}
}
}