java - 我的代码中缺少什么?JAVA
问题描述
这是我的代码块。我正在使用 for 循环遍历字符串,取出一个标记(一个由空格分隔的单词),确定它是否是保留字,如果是,则打印“保留字为:”,否则打印“当前单词是:“。这就是我到目前为止所得到的,我被困在如何使 isKeyWord 工作上。注意:我不能使用拆分功能、扫描仪或标记器。只是一个for循环。
public class Week3Project {
final static String program = "/*\n" +
" * To change this license header, choose License Headers in Project Properties.\n" +
" * To change this template file, choose Tools | Templates\n" +
" * and open the template in the editor.\n" +
" */\n" +
"package testapplication2;\n" +
"\n" +
"import java.util.Scanner;\n" +
"\n" +
"/**\n" +
" *\n" +
" * @author james\n" +
" */\n" +
"public class TestApplication2 {\n" +
"\n" +
" /**\n" +
" * @param args the command line arguments\n" +
" */\n" +
" public static void main(String[] args) {\n" +
" Scanner input = new Scanner(System.in);\n" +
" \n" +
" System.out.println(\"Enter integer #1\");\n" +
" int num1 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #2\");\n" +
" int num2 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #3\");\n" +
" int num3 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #4\");\n" +
" int num4 = input.nextInt();\n" +
" \n" +
" System.out.println(\"Enter integer #5\");\n" +
" int num5 = input.nextInt();\n" +
" \n" +
" //determine the sum\n" +
" int sum = num1 + num2 + num3 + num4 + num5;\n" +
" \n" +
" //this is helpful to make sure your sum is correct\n" +
" System.out.println(\"The sum is: \" + sum);\n" +
" \n" +
" //why doesn't this generate the sum correctly\n" +
" double average = sum / 5;\n" +
" \n" +
" //The average, lets hope its right...\n" +
" System.out.println(\"The average of your numbers is: \" + average);\n" +
" \n" +
" }\n" +
" \n" +
"}\n" +
"";
public static void main(String[] args)
{
String str = program;
String s = "";
String[] keywords = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum",
"extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static",
"strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"};
for (int i = 0; i < str.length(); i++) {
s += str.charAt(i) + "";
if (str.charAt(i) == ' ' || str.charAt(i) == '\t' || str.charAt(i) == '\n') {
String currentWord = s.toString();
//System.out.println(currentWord);
boolean isKeyWord = false;
for (int j = 0; j < keywords.length; j++) {
if (currentWord.equalsIgnoreCase(keywords[j])) {
isKeyWord = true;
}
} break; } }
if (isKeyWord == true) {
System.out.println("Reserved word is: [" + currentWord + "]");
}
else {
System.out.println("Current word is: [" + currentWord + "]")
}
s = "";//Clear the string to get it ready to build next token.
}
}
解决方案
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ' || str.charAt(i) == '\t' || str.charAt(i) == '\n') {
// end of token, check if key word
String currentWord = s.toString();
boolean isKeyword = false
for (int j = 0; j < keywords.length; j++) {
if (currentWord.equalsIgnoreCase(keywords[j])) {
isKeyword = true;
break;
}
}
if(isKeyword) {
System.out.println("Reserved word is: [" + currentWord + "]");
} else {
System.out.println("Current word is: [" + currentWord + "]");
}
s = "";//Clear the string to get it ready to build next token.
} else {
// continue building token
s += str.charAt(i) + "";
}
}
您的循环版本在检查之前会附加空格/制表符/换行符,这会导致当前单词永远不会与您的关键字匹配。