Java循环问题

时间:2014-04-07 21:51:13

标签: java eclipse while-loop

该程序所做的是接收用户输入的单词并以猪拉丁形式返回。翻译循环继续,直到用户输入"退出"。我的问题是,当程序执行并翻译单词时,在输入单词quit之后,它会翻译"退出"这是我不想要的。我知道它翻译的原因"退出"在完成之前,它是一个while while循环,但我仍然坚持如何创建一个while循环功能。我将如何更改程序以便退出"退出"是什么终止循环并且没有被翻译? 例: 字:退出 UIT-qay

    import java.util.Scanner;

public static void main(String[] args) {
    String word;

    Scanner input = new Scanner(System.in);
    do {
        System.out.print("Word: ");
        word = input.next();

        System.out.println(pigLatinWord(word));
        System.out.println();
    } while (!word.equalsIgnoreCase("quit"));
    System.out.println("Translation complete");
}

// --------------------------------------------------------
// Convert one word to pig Latin.
public static String pigLatinWord(String s) {
    String pigWord;
    if (isVowel(s.charAt(0))) {
        pigWord = s + "-way";
    } else if (s.startsWith("th") || s.startsWith("Th")) { // or
                                                            // (s.toUpperCase().startsWith("TH"))
        pigWord = s.substring(2) + "-" + s.substring(0, 2) + "ay";
    } else {
        pigWord = s.substring(1) + "-" + s.charAt(0) + "ay";
    }
    return pigWord;
}

// ---------------------------------------------
// Determines whether c is a vowel character

public static boolean isVowel(char c) {
    String vowels = "aeiouAEIOU";
    return (vowels.indexOf(c) >= 0); // when index of c in vowels is not -1,
                            // c is a vowel
}

}

4 个答案:

答案 0 :(得分:2)

在您有机会检查单词是否等于"退出"之前,您正在执行pigLatinWord(word)。您可以这样更改循环:

do {
    System.out.print("Word: ");
    word = input.next();
    if( "quit".equalsIgnoreCase(word) )
        break;
    System.out.println(pigLatinWord(word));
    System.out.println();
} while (true);

答案 1 :(得分:1)

做{} while();通常不好用,尝试使用while(){}代替。像这样:

Scanner input = new Scanner(System.in);
boolean shouldQuit = false;
while (!shouldQuit) {
    System.out.print("Word: ");
    word = input.next();

    if (word.equalsIgnoreCase("quit")) {
        shouldQuit = true;
    } else {
        System.out.println(pigLatinWord(word));
        System.out.println();
    }
}
System.out.println("Translation complete");

或者,如果您想坚持使用do {},请参阅另一个答案。

答案 2 :(得分:1)

这是一种可能的方式。但是,它不涉及不使用do-while。

//This is an infinite loop, except that we have our exit condition inside the 
//body that'll forcibly break out of the loop.
while (true) {
    System.out.print("Word: ");
    word = input.next();
    if (word.equalsIgnoreCase("quit")) {
        break; //Whelp!  The user wants to quit.  Break the loop NOW
    }

    System.out.println(pigLatinWord(word));
    System.out.println();
}
System.out.println("Translation complete");

答案 3 :(得分:1)

这个有用,我试过了:)

  String word;
  Scanner input = new Scanner(System.in);
  System.out.print("Word: ");
  while(!(word = input.next()).equalsIgnoreCase("quit")) {
     System.out.print("Word: ");
     System.out.println(pigLatinWord(word));
     System.out.println("fsfafa");
   } 
   System.out.println("Translation complete");