Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java程序赢得';t输出字_Java - Fatal编程技术网

java程序赢得';t输出字

java程序赢得';t输出字,java,Java,我正在尝试将文本转换为北约字母表,但我不知道问题出在哪里。我尝试将文本拆分为字符,然后将其放入数组中,然后放入for循环中,以测试字符是否相等,并写出正确的单词 示例文本:您好 结果:利马奥斯卡回声酒店 package text2nato; import java.util.Scanner; public class Text2nato { public static void main(String[] args) { Scanner scan = new Scanner(System.in)

我正在尝试将文本转换为北约字母表,但我不知道问题出在哪里。我尝试将文本拆分为字符,然后将其放入数组中,然后放入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");  
}
}
}
}

package text2nato;
导入java.util.Scanner;
公共类Text2nato{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入要转换到北约的文本:”);
String text=scan.nextLine();
char[]carray=text.toCharArray();

对于(int i=0;i其他人已经在评论中指出,您正在将字符串与字符进行比较,而字符永远不会相等。为了进行说明,请尝试以下程序:

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

    System.out.println(x);
  }
}
结果将是
false
。您可能会认为这在Java中有点“gotcha”,但这是一个意见问题

此外,如果一行中有那么多
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;
      }
    }
  }
}
package text2nato;
导入java.util.Scanner;
公共类Text2nato{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入要转换为nato:”)的文本;
String text=scan.nextLine();
//您可能需要考虑将整个字符串转换为小写以使该情况不敏感。
char[]carray=text.toCharArray();
对于(int i=0;i0)
{
//我们需要在这里预留一个空间
系统输出打印(“”);
}
开关(卡雷[i])
{
案例“a”:系统输出打印(“alpha”);中断;
案例“b”:系统输出打印(“bravo”);中断;
//你剩下的案子都在这里
//确保处理用户输入无效内容的情况
默认值:System.out.print(carray[i]+“不是有效的小写字母”);break;
}
}
}
}

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

欢迎使用Stack Overflow!看起来您可能需要学习使用调试器。请自行解决一些问题。如果以后仍有问题,请将问题具体化有了您需要的帮助。没有任何
“string”
将等于一个字符。您应该使用
'a'==carray[i]
等。此外,您显然应该重构您的代码。它甚至看起来不像代码,而更像一个扭曲的JSON文件。这是非常冗余的。那么,当您输入“hello”时,您当前得到了什么输出?与错误无关,但请使用地图或其他东西,而不是这堵if语句墙。我不理解投票结果。代码在那里。输入在那里。预期输出在那里。意外结果被清楚地描述(“不会输出单词”)。不明白这如何被视为没有mcve。可能是一个简单的打字错误(重复26次…)。