[英]Can't spot “Unreachable code”
我的程序中有些东西没有意义。 可能是我的while语句嵌入了几个不同的if语句。 但是主要错误是在我的while循环之后指出的,我似乎无法弄清楚。 我评论了错误所在。
该程序如下所示:
import java.util.*;
import java.io.*;
class MorseCode
{
public static void main (String [] args) throws FileNotFoundException
{
Scanner keyboard = new Scanner(new File("myfile.txt"));
String text = " ";
while (keyboard.hasNextLine())
{
text = keyboard.nextLine();
morse(text); // Goes through morse(text);
String code = ""; // String is declared
code = morse(text);
System.out.println(code); // prints the code which is being called upon morse(text)
}
keyboard.close();
}
static String morse(String text)
{
String code = "";
int i = 0;
while (true) {
if (text.charAt(i) == 'a' || text.charAt(i) == 'A')
System.out.print(".-");
if (text.charAt(i) == 'b' || text.charAt(i) == 'B')
System.out.print("-...");
if (text.charAt(i) == 'c' || text.charAt(i) == 'C')
System.out.print("-.-.");
if (text.charAt(i) == 'd' || text.charAt(i) == 'D')
System.out.print("-..");
if (text.charAt(i) == 'e' || text.charAt(i) == 'e')
System.out.print(".");
if (text.charAt(i) == 'f' || text.charAt(i) == 'F')
System.out.print("..-.");
if (text.charAt(i) == 'g' || text.charAt(i) == 'G')
System.out.print("--.");
if (text.charAt(i) == 'h' || text.charAt(i) == 'H')
System.out.print("....");
if (text.charAt(i) == 'i' || text.charAt(i) == 'I')
System.out.print("..");
if (text.charAt(i) == 'j' || text.charAt(i) == 'J')
System.out.print(".---");
if (text.charAt(i) == 'k' || text.charAt(i) == 'K')
System.out.print("-.-");
if (text.charAt(i) == 'l' || text.charAt(i) == 'L')
System.out.print(".-..");
if (text.charAt(i) == 'm' || text.charAt(i) == 'M')
System.out.print("--");
if (text.charAt(i) == 'n' || text.charAt(i) == 'N')
System.out.print("-.");
if (text.charAt(i) == 'o' || text.charAt(i) == 'O')
System.out.print("---");
if (text.charAt(i) == 'p' || text.charAt(i) == 'P')
System.out.print(".--.");
if (text.charAt(i) == 'q' || text.charAt(i) == 'Q')
System.out.print("--.-");
if (text.charAt(i) == 'r' || text.charAt(i) == 'R')
System.out.print(".-.");
if (text.charAt(i) == 's' || text.charAt(i) == 'S')
System.out.print("...");
if (text.charAt(i) == 't' || text.charAt(i) == 'T')
System.out.print("-");
if (text.charAt(i) == 'u' || text.charAt(i) == 'U')
System.out.print("..-");
if (text.charAt(i) == 'v' || text.charAt(i) == 'V')
System.out.print("...-");
if (text.charAt(i) == 'w' || text.charAt(i) == 'W')
System.out.print(".--");
if (text.charAt(i) == 'x' || text.charAt(i) == 'X')
System.out.print("-..-");
if (text.charAt(i) == 'y' || text.charAt(i) == 'Y')
System.out.print("-.--");
if (text.charAt(i) == 'z' || text.charAt(i) == 'Z')
System.out.print("--..");
if (text.charAt(i) == '1')
System.out.print(".----");
if (text.charAt(i) == '2')
System.out.print("..---");
if (text.charAt(i) == '3')
System.out.print("...--");
if (text.charAt(i) == '4')
System.out.print("....-");
if (text.charAt(i) == '5')
System.out.print(".....");
if (text.charAt(i) == '6')
System.out.print("-....");
if (text.charAt(i) == '7')
System.out.print("--...");
if (text.charAt(i) == '8')
System.out.print("---..");
if (text.charAt(i) == '9')
System.out.print("----.");
if (text.charAt(i) == '0')
System.out.print("-----");
if (text.charAt(i) == '-')
System.out.print("...");
if (text.charAt(i) == ' ')
System.out.print(".......");
i++;
}
if (i < text.length()); //Unreachable code
return code;
}
}
将支架向下移动一个(在i ++之后),因此如下所示:
if (text.charAt(i) == '0')
System.out.print("-----");
if (text.charAt(i) == '-')
System.out.print("...");
if (text.charAt(i) == ' ')
System.out.print(".......");
i++;
if (i < text.length()) //Reachable code now :)
return code;
}
}
}
这样一来,您实际上可以到达检查是否准备爆发的地方
您还可以将while循环的条件从true更改为(i <text.length()),但是当前代码建议您尝试执行上述操作,但只是放了一个括号:)
另外,不确定您要在这里做什么? 您要打印还是退回? 代码的值永远不会从“”更改,但您仍会返回该值。 我认为您应该将代码替换为code + =,以便实际上返回适当的内容,然后可以执行System.out.println(morse(“ Something”));。
您的while循环永远不会终止。
如果要遍历文本字符串中的字符,建议您进行以下更改:
while (true) {
至
while (i < text.length()) {
然后在while循环完成后简单地返回code
。
我还建议您使用if-else语句,而不要使用一系列的if语句,这样就不必对每个字符都评估所有if条件。
这是你的while(true)
。 循环永远不会结束,因此永远不会到达循环之后的代码。
使用设置为true的布尔变量。
boolean x = true;
while (x) {...}
您使用while (true) {
-这将永远不会终止。
如果您想使用:
while (keyboard.hasNext()) {
那么您将需要将Scanner实例传递给您的morse方法。 通常,尽管最好使此方法仅做一项工作-翻译。 为什么不使用Map<String, String>
来存储您的莫尔斯翻译,并使用带有translateMorse方法?
您的程序将永久停留在while(true)循环中,因为根据定义,true条件始终为true。
你的while循环应该是
while (i < text.length()) {
并且应删除无法访问的if语句,您应该在那里执行
return code;
1.您的代码将永远不会在Infinite while loop
终止 。
2.你用while( i<text.length() )
代替 while(true)
3.我进一步不建议使用if
或if-else
阶梯, if-else
要检查2到3个条件,可以使用它,但是对于上面的示例 ,请使用switch statement
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.