我想测试当前char 当前是不是',',' - ','。'或 '' 是否有一个较短的表达式:短条件下,java
if((current != ' ') || (current != '.') || ...)
什么想法?
编辑:
我只允许使用方法nextChar和getchar函数。我必须通过字符循环。
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class WoerterZaehlen {
public static void main(String[] args) {
int wordCount = 0;
/* Ab hier dürft ihr eigenen Code einfügen */
char previous = ' ';
while(hasNextChar()){
char current = getChar();
if(current !=)
//if(((current == ' ') || (current == '.') || (current == ',')) && ((previous != ' ') && (previous != ','))){
// wordCount++;
//}
previous = current;
}
/* Ab hier dürft ihr nichts mehr ändern. */
System.out.println("Anzahl der Wörter: " + wordCount);
}
private static InputStreamReader reader;
private static int next = -1;
public static boolean hasNextChar() {
if(next != -1)
return true;
try {
if(reader == null)
reader = new InputStreamReader(new FileInputStream("textdatei.txt"));
next = reader.read();
} catch (IOException e) {
System.out.println("Datei wurde nicht gefunden.");
}
return next != -1;
}
public static char getChar() {
char c = (char) next;
next = -1;
return c;
}
}
我想你的意思是&&而不是||。 – 2010-10-28 05:33:26
你为什么用“优化”来标记?最短的表达式在性能方面可能不是最优的。 – 2010-10-28 05:36:11
我看到了你的编辑。我认为你需要使用Scanner类。它已经具有nextChar和getChar的功能。 – Emil 2010-10-28 06:37:26