Java 字符串开关语句不工作

Java 字符串开关语句不工作,java,loops,switch-statement,Java,Loops,Switch Statement,这个程序的目的是把摩尔斯电码翻译成英语。我将程序的执行逻辑分为三种情况:一个莫尔斯电码字符、两个字符和三个以上字符的翻译。这个程序试图翻译第三种情况下的字符。如果您能提供帮助,并决定测试该程序,请输入至少四个摩尔斯电码字符(介于a和d之间)!否则,请期待一些边界外的异常 我用的开关怎么了?我读过,直到几年前,Java还不支持使用字符串的switch语句。Java现在支持字符串开关语句,如果我没有弄错的话,我的语法与这些语句的不太复杂的约定是一致的。为了确定编译器是否首先识别开关的主题(即开关(

这个程序的目的是把摩尔斯电码翻译成英语。我将程序的执行逻辑分为三种情况:一个莫尔斯电码字符、两个字符和三个以上字符的翻译。这个程序试图翻译第三种情况下的字符。如果您能提供帮助,并决定测试该程序,请输入至少四个摩尔斯电码字符(介于a和d之间)!否则,请期待一些边界外的异常


我用的开关怎么了?我读过,直到几年前,Java还不支持使用字符串的switch语句。Java现在支持字符串开关语句,如果我没有弄错的话,我的语法与这些语句的不太复杂的约定是一致的。为了确定编译器是否首先识别开关的主题(即
开关(AreReferenceThatPointsToAstring)
,我在开关前面的一行中打印了引用。没有问题。我还研究了如果开关的主题被显式声明,是否可以执行案例。而不是说
开关(array3[i])
,我在
switch(“.”)
中写道,这个测试确实导致了一个工作开关,给我的印象是
switch(array3[I])
switch(aStringexpression)

import java.util.Scanner;
公共类MorseToEnglish1{
公共静态void main(字符串[]args){
System.out.println(“在这里输入莫尔斯”);
扫描仪输入=新扫描仪(System.in);
String container=input.nextLine();
char[]数组=container.toCharArray();
整数计数=0;
for(int i=0;i
通过查看代码,您有太多的
}
大括号。您只需要在switch语句末尾使用其中一个大括号

case ("._"):
        {System.out.println("a");
        break;
    case "_...":
        {System.out.println("b");
        break;
    case "_._.":
        {System.out.println("c");
        break;
    case "_..":
        {System.out.println("d ");
        break;
        }

它应该可以工作。我的假设是,你的问题当然是你的代码没有编译。我很确定你发布的代码不会编译。

你的
switch
语句没有问题,程序的其余部分有很多错误

问题是,自定义“拆分”过程在文本中留下空格,因此,如果要输入
。。。。。。。。。。
,数组将拆分为

" _...", " _._."
这在两个帐户上是错误的,一个,它缺少我输入的内容,另一个,它在
字符串
中留下了前导空格字符,所以当你尝试比较
开关
语句中的
字符串
时,你实际上是在尝试做类似于

"_...".equals(" _...")
这将失败

首先,这个“手动”拆分代码

char[] array = container.toCharArray();
int count = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] == ' ') {
        count++;
    }
}

// count counts the number of times a space appears
System.out.println(count);
int[] array2 = new int[count];
int counter = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] == ' ') {
        array2[counter] = i;
        counter++;
    }
}

// array2 assigns the indexes of spaces to its members
System.out.println(counter);

String[] array3 = new String[array2.length - 1];
for (int i = 0; i < array3.length; i++) {
    array3[i] = container.substring(array2[i], array2[i + 1]);
}
// array3 creates substrings based on these spaces
System.out.println(array3[1]);
System.out.println(array3[0]);

坦率地说,我不知道它想要实现什么,如果输入
字符串
上的“代码”少于4个,并且无论如何都不能正确解析输入(丢弃值),它就会崩溃

您的拆分代码留下了前导空格字符,这会导致前导空格字符不正确匹配。您应该改用的版本来生成数组3。这将以更少的工作量创建正确的子字符串

编辑:
您的拆分代码似乎还保留了第一个和最后一个莫尔斯代码,这就是为什么当您输入少于4时,会得到ArrayIndexOutofBoundsSeptions。

除非您使用Java 8,否则无法在中打开字符串Java@Eenvincible:您是指Java 7。所以请具体说明。您在哪里看到问题?当我运行此代码时,我会得到一个错误在
开关之前
。你在输入数据时有什么特别的方法吗?我完全同意@Makoto。你能先解释一下你的逻辑吗?你似乎把一切都复杂化了。我必须承认,我必须克服一种压倒性的冲动,自动否决任何标题为“[某些语言功能]不起作用”的帖子。我必须提醒自己,我确实在当天针对IBM的PL/1编译器提交了一份伪造的编译器错误报告(但我确实在编译器中发现了3个真正的错误)。然而,你留下了所有开头的大括号吗?
char[] array = container.toCharArray();
int count = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] == ' ') {
        count++;
    }
}

// count counts the number of times a space appears
System.out.println(count);
int[] array2 = new int[count];
int counter = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] == ' ') {
        array2[counter] = i;
        counter++;
    }
}

// array2 assigns the indexes of spaces to its members
System.out.println(counter);

String[] array3 = new String[array2.length - 1];
for (int i = 0; i < array3.length; i++) {
    array3[i] = container.substring(array2[i], array2[i + 1]);
}
// array3 creates substrings based on these spaces
System.out.println(array3[1]);
System.out.println(array3[0]);
String[] array3 = container.split(" ");