所以我在这里有这个代码,它计算给定短语中的元音。如果用户说是,我试图让它重复

时间:2014-11-08 02:54:55

标签: java loops repeat

我想知道如果用户确实以1响应,我怎么能让程序结束重复。我需要重新组织它以使它成为if语句的一部分吗?

Scanner input = new Scanner(System.in);

System.out.println("Count Vowels \n============");
System.out.println("Type a sentence and this program will tell you\n\nhow many vowels there are (excluding 'y'):");
String string1;

string1 = input.nextLine();
string1 = string1.toLowerCase();
int vowels = 0;
int answer;
int i = 0;

for (String Vowels : string1.split(" ")) {
    for (i = 0; i < Vowels.length(); i++) {

        int letter = Vowels.charAt(i);

        if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
            vowels++;
        }
    }

    System.out.println(Vowels.substring(0, 1).toUpperCase() + Vowels.substring(1) + " has " + vowels + " vowels");
    vowels = 1;
}

System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
answer = input.nextInt();

if (answer == 1) {
    System.out.println("You have chosen to count the vowels in another phrase");
} else {
    System.out.println("Have a nice day");
}

5 个答案:

答案 0 :(得分:0)

您可以使用do/while循环执行此操作。这种循环的骨架如下所示:

Scanner input = new Scanner(System.in);
do {
    // do your stuff here

    System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
} while(input.nextInt() == 1);

System.out.println("Have a nice day");

它询问用户并在while(input.nextInt() == 1)语句中评估输入的数字。如果此比较返回true(即用户输入1),则循环再次开始。如果不是(即用户输入的内容不是1),则循环停止,您将收到“Good Bye”消息。

答案 1 :(得分:0)

有很多方法可以做到这一点。搜索Google可以在更短的时间内找到正确的答案,而不是提出问题。但是,既然你花时间问这个问题就是答案:

import java.util.Scanner;

public class Driver {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int answer = 0;
        System.out.println("Count Vowels \n============");

        // the do-while loop ensures that the code is executed at least once
        do {
            // on the first run answer equals zero, but on other runs it will equal one
            if(answer == 1) {
                System.out.println("You have chosen to count the vowels in another phrase");
            }

            System.out.println("Type a sentence and this program will tell you\n\nhow many vowels there are (excluding 'y'):");
            String string1;

            string1 = input.nextLine();
            string1 = string1.toLowerCase();

            int vowels = 0;
            int i = 0;

            for (String Vowels : string1.split(" ")) {
                for (i = 0; i < Vowels.length(); i++) {

                    int letter = Vowels.charAt(i);
                    if (letter == 'a' || letter == 'e' || letter == 'i'
                            || letter == 'o' || letter == 'u') {
                        vowels++;
                    }
                }
                System.out.println(Vowels.substring(0, 1).toUpperCase()
                        + Vowels.substring(1) + " has " + vowels + " vowels");
                vowels = 1;
            }

            System.out.println("Would you like to check another phrase in the Vowel Counter? If so type 1 if not type 2 and press enter");
            answer = input.nextInt();
        } while (answer == 1);

        System.out.println("Have a nice day");
    }

}

在你的代码中,如果字母在a, e, i, o and u集合中,则断言字母是元音。但是,在某些情况下,y字母可以是元音。

  

通常,当音节已经有元音时,Y是辅音。此外,当Y用于代替柔和的J声音时,Y被认为是辅音,例如Yolanda或Yoda。   在Bryan和Wyatt的名字中,Y是元音,因为它为两个名字的第一个音节提供唯一的元音。对于这两个名字,字母A是第二个音节的一部分,因此不会影响Y的性质。

您可以通过检查字母y是否为元音来进一步扩展您的代码。

答案 2 :(得分:0)

您可以将其拆分为多个方法,并使用一个主方法调用while循环内的其他方法。例如:

boolean continueCounting = false;

void countingVowels() {

 //some start game method to make continueCounting = true
 //something like "press 1 to start"
 //if (input == 1) { continueCounting = true; }

 while(continueCounting) {

  String userInput = getUserInput(); 
  countVowels(userInput); //count vowels in word from user input and prints them out to console
  getPlayAgainDecision(); //ask user to put 1 or 2

  if (answer == 1) {
   continue
  } else if (answer == 2) {
   continueCounting = false;
  } else {
   System.out.println("incorrect input, please choose 1 or 2");
  }
 }
}

答案 3 :(得分:0)

这是一种更优雅的计数方式(我更新了代码以满足Johnny的评论,我之前的回答没有回答OP的问题。代码现在循环没有不必要的代码):

public static void main(String... args)
{
    int answer = 0;
    Scanner input = null;
    do
    {
        input = new Scanner(System.in);
        System.out.print("Type a sentence and this program will tell you\nhow many vowels there are (excluding 'y'):");
        String sentence = input.nextLine();

        int vowels = 0;
        String temp = sentence.toUpperCase();
        for (int i = 0; i < sentence.length(); i++)
        {
            switch((char)temp.charAt(i))
            {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    vowels++;
            }
        }
        System.out.println("The sentence: \"" + sentence + "\" has " + vowels + " vowels");
        System.out.print("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press any other key... ");
        String tempNum = input.next();
        try
        {
            answer = Integer.parseInt(tempNum);
        } catch (NumberFormatException e)
        {
            answer = 0;
        }
        System.out.println();
    } while (answer == 1);
    input.close();
    System.out.println("Have a nice day");
}

请注意,最后,我捕获了一个NumberFormatException,以便更好地验证用户的输入。

答案 4 :(得分:0)

只需将主for循环放在do-while循环中,如下所示:

do
{
    for (String Vowels : string1.split(" ")) {
        for (i = 0; i < Vowels.length(); i++) {

            int letter = Vowels.charAt(i);

            if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
                vowels++;
            }
        }

    System.out.println(Vowels.substring(0, 1).toUpperCase() +
        Vowels.substring(1) + " has " + vowels + " vowels");
    vowels = 1;

    System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
    answer = input.nextInt();
    }
} while (answer == 1);
System.out.println("Have a nice day");

此外,有更好的方法可以进行计数,例如:

for (char c : string1.toCharArray())
{
    c = Character.toLowerCase(c);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
        count++;
}