让程序重复(更多然后进入)

时间:2016-09-27 01:43:42

标签: java

如何让它循环以查看它们是否有多于一个词要翻译?我认为我已经走上了正确的道路,重复它,不得不错过一个我无法看到的小细节。

谢谢!

所以这是我的代码:

import java.util.Scanner;

public class holdingCode
{
   public static void main (String[] args)
   {

        System.out.println("Please enter a word to be translated: ");
        Scanner in = new Scanner(System.in);



        //Grabs a word for the user to translate further down

    String answer = "";        
   do
   { 
        String newWord = in.nextLine();
        newWord = newWord.toUpperCase(); 

      char vowel = Character.toLowerCase(newWord.charAt(0));

      if (vowel == 'a' || vowel == 'e' || vowel == 'i' || vowel == 'o' || vowel == 'u')
      {
         String pigLatinWay = newWord + "WAY";
         System.out.println("Your word in pig latin is " + pigLatinWay + ".");
         System.out.println("Do you have another word to translate? (Y/N)");
         answer = in.nextLine();  
         //converts if it is a vowel
       }//end of if code   

       else 
       {
          if (true)
          {
             String first = newWord.substring(0,1);
             String slice = newWord.substring(1,newWord.length());
             System.out.println(" Your translated word is " + slice + first + "AY"); 
             System.out.println("Do you have another word to translate? (Y/N)");
             answer = in.nextLine();             
             //converts if it isn't a vowel
          }//end of if code
        }//end of else code

    }

    while (answer.equalsIgnoreCase("Y"));

    }//end of pigLatin


}//end of program

1 个答案:

答案 0 :(得分:0)

String anwser = in.nextLine();的范围仅限于

中定义的块

所以试试

newWord = newWord.toUpperCase();
String answer = "";
块中的

anwser = in.nextLine();      

最后你的比较

while (anwser.equalsIgnoreCase("Y"));

修改

总结一下,你可以做到

  String answer;

    do 
    {
        String newWord = in.nextLine();
        newWord = newWord.toUpperCase();