Java程序不会输出单词

时间:2019-01-02 21:55:33

标签: java

我正在尝试将文本转换为北约字母,但是我不知道是什么问题。我试图将文本拆分为字符,然后将其放入数组,然后在for循环中测试字符是否相等,并输入正确的单词

示例文本:您好

结果:酒店呼应利马利马奥斯卡

package text2nato;
import java.util.Scanner;
public class Text2nato {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

System.out.println("Enter the text to conver to nato :");
String text = scan.nextLine();
char[] carray = text.toCharArray();

for(int i=0;i<carray.length;i++){
if("a".equals(carray[i])){
System.out.print("alpha");
}if("b".equals(carray[i])){
System.out.print("brabo");
}if("c".equals(carray[i])){
System.out.print("charlie");
}
if("d".equals(carray[i])){
System.out.print("delta");
}if("e".equals(carray[i])){
System.out.print("echo");
} if("f".equals(carray[i])){
System.out.print("foxtrot");
}if("g".equals(carray[i])){
System.out.print("golf");
} if("h".equals(carray[i])){
System.out.print("hotel");
} if("i".equals(carray[i])){
System.out.print("india");
} if("j".equals(carray[i])){
System.out.print("juliet");
} if("k".equals(carray[i])){
System.out.print("kilo");
} if("l".equals(carray[i])){
System.out.print("lima");
} if("m".equals(carray[i])){
System.out.print("mike");
} if("n".equals(carray[i])){
System.out.print("november");
} if("o".equals(carray[i])){
System.out.print("oscar");
} if("p".equals(carray[i])){
System.out.print("papa");
} if("q".equals(carray[i])){
System.out.print("quebec");
} if("r".equals(carray[i])){
System.out.print("romeo");
} if("s".equals(carray[i])){
System.out.print("sierra");
} if("t".equals(carray[i])){
System.out.print("tango");
} if("u".equals(carray[i])){
System.out.print("uniform");
} if("v".equals(carray[i])){
System.out.print("victor");
} if("w".equals(carray[i])){
System.out.print("whiskey");
} if("x".equals(carray[i])){
System.out.print("x-ray");
} if("y".equals(carray[i])){
System.out.print("yankee");
} if("z".equals(carray[i])){
System.out.print("zulu");  
}
}
}
}

1 个答案:

答案 0 :(得分:2)

其他人已经在注释中指出,您正在将字符串与char比较,这将永远是不相等的。作为说明,请尝试以下程序:

public class Demo {
  public static void main(String[] args) {
    Boolean x = "b".equals('b');

    System.out.println(x);
  }
}

结果将为false。您可能会争辩说这在Java中有点“陷阱”,但这是观点的问题。

此外,如果您连续有这么多if条语句,这是一个很好的暗示,表明可能有问题。至少,switch语句更容易阅读:

package text2nato;
import java.util.Scanner;

public class Text2nato {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter the text to convert to nato: ");
    String text = scan.nextLine();

    // You might want to consider converting the whole string to lowercase to make this case-insensitive
    char[] carray = text.toCharArray();

    for(int i=0; i < carray.length; i++){
      if (i > 0)
      {
         // We need to prepend a space here
         System.out.print(" ");
      }
      switch (carray[i])
      {
        case 'a': System.out.print("alpha"); break;
        case 'b': System.out.print("bravo"); break;
        // The rest of your cases go here
        // Be sure to handle the case where the user enters something invalid
        default: System.out.print(carray[i] + " is not a valid lowercase letter"); break;
      }
    }
  }
}

如您所见,正确缩进代码,添加一些额外的空格并使用switch语句使该很多更加容易阅读。