Java 中 char 数组的位数随相同输入而变化
Count of digits from char array varies with same input in Java
我需要从控制台加载大量数字,用空格分隔,我的结果应该计算输入中的数字数。这些可以是从 0 到 10^9 的所有数字。
下面的代码适用于较小的输入,但对于文件 here 中的数据,它显示错误的结果,而且每次执行都不同(对于附件,它应该是 968364)。
我做错了什么?我不能在循环中使用任何方法或 类,它必须在原始类型上工作,但显然我遗漏了一些东西。
请帮忙,提前感谢任何提示。
int input_size = 0;
char[] input = new char[1000000];
InputStreamReader in = new InputStreamReader(System.in);
in.read(input);
for(int a = 0; a<input.length; a++){
if (input[a] >= 48 && input[a] <= 57){
input_size ++;
}
}
System.out.println(input_size);
一些事情,首先是 in.read()
returns the number of characters it read. You need to store that, because the default value for an int
(in an int[]
is 0
). Next, you can use '0'
and '9'
instead of hardcoding your valid ascii values - but I would prefer Character.isDigit(char)
。喜欢,
InputStreamReader in = new InputStreamReader(System.in);
int readLen = in.read(input);
for (int a = 0; a < readLen; a++) {
if (Character.isDigit(input[a])) {
input_size++;
}
// if (input[a] >= '0' && input[a] <= '9') {
// input_size++;
// }
}
System.out.println(input_size);
您没有阅读整个文件,我编辑了您的代码,因此它以 1000 个字符为单位读取输入,直到完成
public static void main(String[] args) throws IOException {
char[] input = new char[1000];
int input_size = 0;
int readChars;
//Reader in = new InputStreamReader(System.in);
Reader in = new FileReader("572be-stud3.txt");
do {
readChars = in.read(input);
for (int a = 0; a < readChars; a++) {
if (input[a] >= 48 && input[a] <= 57) {
input_size++;
}
}
} while((readChars < 0) || (input[readChars-1] != '\n'));
in.close();
System.out.println(input_size);
}
我需要从控制台加载大量数字,用空格分隔,我的结果应该计算输入中的数字数。这些可以是从 0 到 10^9 的所有数字。
下面的代码适用于较小的输入,但对于文件 here 中的数据,它显示错误的结果,而且每次执行都不同(对于附件,它应该是 968364)。
我做错了什么?我不能在循环中使用任何方法或 类,它必须在原始类型上工作,但显然我遗漏了一些东西。
请帮忙,提前感谢任何提示。
int input_size = 0;
char[] input = new char[1000000];
InputStreamReader in = new InputStreamReader(System.in);
in.read(input);
for(int a = 0; a<input.length; a++){
if (input[a] >= 48 && input[a] <= 57){
input_size ++;
}
}
System.out.println(input_size);
一些事情,首先是 in.read()
returns the number of characters it read. You need to store that, because the default value for an int
(in an int[]
is 0
). Next, you can use '0'
and '9'
instead of hardcoding your valid ascii values - but I would prefer Character.isDigit(char)
。喜欢,
InputStreamReader in = new InputStreamReader(System.in);
int readLen = in.read(input);
for (int a = 0; a < readLen; a++) {
if (Character.isDigit(input[a])) {
input_size++;
}
// if (input[a] >= '0' && input[a] <= '9') {
// input_size++;
// }
}
System.out.println(input_size);
您没有阅读整个文件,我编辑了您的代码,因此它以 1000 个字符为单位读取输入,直到完成
public static void main(String[] args) throws IOException {
char[] input = new char[1000];
int input_size = 0;
int readChars;
//Reader in = new InputStreamReader(System.in);
Reader in = new FileReader("572be-stud3.txt");
do {
readChars = in.read(input);
for (int a = 0; a < readChars; a++) {
if (input[a] >= 48 && input[a] <= 57) {
input_size++;
}
}
} while((readChars < 0) || (input[readChars-1] != '\n'));
in.close();
System.out.println(input_size);
}