3

// 我的 else if 语句需要帮助。如果单词以元音开头,则在单词末尾添加方式。如果单词以辅音开头,则将辅音放在末尾并添加ay。

我的问题是,如果我有一个以元音作为第一个字母的单词,它就好像它有一个辅音一样贯穿。如果我输入“are”,我会得到“arewayreaay”而不是“areway”。

public class piglatin {
    public static void main(String[] args) {
        String str = IO.    readString();    
        String answer = "";

        if(str.startsWith("a"))
            System.out.print(str + "way");

        if(str.startsWith("e"))
            System.out.print(str + "way");

        if(str.startsWith("i"))
            System.out.print(str + "way");

        if(str.startsWith("o"))
            System.out.print(str + "way");

        if(str.startsWith("u"))
            System.out.print(str + "way");
        else{
            char i = str.charAt(0);
            answer = str.substring( 1, str.length());
            System.out.print(answer + i + "ay");
        }
    }
}
4

4 回答 4

6
if(str.startsWith("a") || str.startsWith("e") || str.startsWith("i") ... (and so on)) {
  System.out.print(str + "way"); 
} else{
  char i = str.charAt(0);
  answer = str.substring( 1, str.length());
  System.out.print(answer + i + "ay");
}

or if / else if / else

Explanation:

//  combine all the if blocks together .. if you dont it checks for vowel 'a' and prints
//  'areay' then it checks for vowel 'u' and enters the else block .. where it again
//  operates on 'are' (check immutability of strings) gets charAt(0) i.e 'a' and prints
//  'rea' and since you concatenated 'ay' ... the final output = 'arewayreaay'
于 2012-10-17T21:02:59.057 回答
2

Note that each of your leading if cases does the same work, and so you can instead collapse all of those into a single branch:

if (str.startsWith("a") || str.startsWith("e") || ...
    System.out.print(str + "way");
else
{
    // do the substring and append "ay"

Aside from collapsing those branches, your problem is that each of your if statements after the first needs an else:

if(str.startsWith("a"))
    System.out.print(str + "way");
else if(str.startsWith("e"))
    System.out.print(str + "way");
// ...

You only want one of your branches to execute. But consider what happens in your final if/else: you've already modified e.g., "are" as the result of your first if statement. Since you aren't using chained if/else, you'll get to your final set of tests:

if(str.startsWith("u"))
    System.out.print(str + "way");
else{

And you'll end up in the else case, since your string doesn't start with "u". But you've already handled the string in an earlier if statement.

于 2012-10-17T21:03:27.343 回答
1

What you have here is not a switch - the else is only attaching to the final if. Replace each if with else if, other than the first one, or combine the conditions like jakub suggested.

于 2012-10-17T21:03:24.190 回答
1

我认为你的花括号搞砸了。它为 'a' 执行“if starts with”,但随后也为“u”执行“else”……所以你得到了两者。你可能的意思是:

public static void main(String[] args) 
{
    String str = IO.readString();
    String answer = "";

    if (str.startsWith("a"))
         System.out.print(str + "way");
    else if(str.startsWith("e"))
        System.out.print(str + "way");
    else if(str.startsWith("i"))
        System.out.print(str + "way");
    else if(str.startsWith("o"))
        System.out.print(str + "way");
    else if(str.startsWith("u"))
        System.out.print(str + "way");
    else
    {
        char i = str.charAt(0);
        answer = str.substring( 1, str.length());
        System.out.print(answer + i + "ay");
    }
}
于 2012-10-17T21:07:21.900 回答