2014-11-08 14 views
-1

我想知道如何讓程序結束重複,如果用戶確實迴應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"); 
} 
+0

不清除'Scanner'緩衝也許? – Azar 2014-11-08 02:58:25

+0

你應該看看'do/while'循環是什麼以及它們是如何工作的。然後你可以在用戶輸入'1'的時候重複你的程序。 – Tom 2014-11-08 02:58:30

+0

您可以使用while循環...只要用戶輸入1,程序就會繼續,否則程序將跳出循環。 – 2014-11-08 03:02:52

回答

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」消息。

+0

謝謝你回答真的有幫助,但是當我運行程序時,我得到了這個錯誤java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:1 – dappers 2014-11-08 03:44:08

+0

@dappers如果您嘗試訪問大於字符串的長度。例如這裏'Vowels.substring(0,1)'(如果'Vowels'是空的)或這裏'Vowels.charAt(i)'(如果'i'是'1'並且'元音符只包含字符,在索引'0')。 (char c:sentence.toCharArray())switch(c){'case'a': case'e': – Tom 2014-11-08 03:59:35

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是元音您的代碼擴展或不。

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"); 
    } 
} 
} 
0

這是一個更優雅的方式做計數(我更新的代碼,以滿足約翰尼的評論,我以前的答案沒有回答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,以便對用戶輸入進行更健壯的驗證。

+0

很好的方法來計算 – 2014-11-08 03:25:38

+1

一個更優雅的方法,而不需要強制轉換。 case'i': case'o': case'u': count ++; 休息; 默認值: //無計數增量 } }' – 2014-11-08 03:31:27

+1

另外,OP的問題是要根據用戶輸入獲取重複的代碼。不是一個優雅的計數方法。 – 2014-11-08 03:34:42

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++; 
} 
相關問題