2015-05-13 33 views
1

我試圖創建一個接受用戶輸入的程序,掃描每個子字符串並檢查該字符是否匹配字母表中的某個位置,想要在分數字符串中獲得相同的位置以獲得該字符的分數並顯示每個字符和總分數的分數。問題爲輸入的每個字符分配一個值並顯示輸入的值

我在我的代碼下面應該已經完成​​了我想要做的每個部分,但是第二個for循環出現了問題,我找不到解決方案,我認爲它可能與子字符串在圈內,但我不知道。

import java.util.*; 
import java.io.*; 
public class testt 
{ 
    public static void main(String [] args) throws IOException 
    { 
     String alpha = "abcdefghijklmnopqrstuvwxyz"; 
     String score = "13321424185131139111144849"; 
     String result = ""; 
     int value = 0, total = 0; 
     Scanner in = new Scanner(System.in); 
     System.out.print("Enter word: "); 
     String input = in.nextLine(); 
     if(input == null || input.length() == 0) 
     { 
      System.out.println("Enter input."); 
      System.exit(0); 
     } 
     String inputLower = input.replaceAll("[^a-zA-Z ]", "").toLowerCase(); 
     String inputArray[] = inputLower.split("\\s+"); 
     for(int i = 0; i < alpha.length(); i++) 
      if(inputLower.substring(i, i + 1) == alpha.substring(i, i + 1)) 
      { 
       value = Integer.parseInt(score.substring(i, i + 1)); 
       if(value == 9) 
        value++; 
       result += inputLower.substring(i, i + 1) + " will give you " + value + " point(s)\n"; 
       total += value; 
      } 
      else if(i == alpha.length() + 1) 
      { 
       System.out.println(result + "This word will earn you: " + total); 
       System.exit(0); 
      } 

    } 
} 

任何意見或提示我真的很感激,因爲我認爲我的程序大部分已完成。

下面是我從http://i.imgur.com/ftfcINX.jpg

回答

0

研究既然你可以在inputArray多個單詞的問題,你需要兩個嵌套的循環,而不是一個單一的循環。循環應該走inputArray一個接一個,則該數組內它應該走的英文字母,做打分:

String alpha = "abcdefghijklmnopqrstuvwxyz"; 
//    a b c d e f g h i j k l m n o p q r s t u v w x y z 
int[] score = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10}; 
int total = 0; 
for (String word : inputArray) { 
    int wordTotal = 0; 
    for(int i = 0; i < word.length(); i++) { 
     char letter = word.charAt(i); 
     int pos = alhpa.indexOf(letter); 
     int value = score[pos]; 
     System.out.println("Letter "+letter+" will earn you: " + value); 
     // Increment wordTotal, not total 
     wordTotal += value; 
    } 
    System.out.println("Word "+word+" will earn you: " + wordTotal); 
    total += wordTotal; 
} 
System.out.println("Total for all words: " + total); 

這裏有幾個實現注意事項:

  • 使用數組整數,而不是一個字符數組可以讓你避免「編碼」十個'9',並幫助你刪除if (value == 9)黑客。
  • 您可以使用indexOf代替char,而不是使用子字符串和查找字符串中的單詞,並使用該索引查找分數數組。
0

我想我的兩分錢,我是升職較晚。 你可以把實際得分的代碼放到一個函數並調用它在你的循環

public class Test { 
    public static void main(String[] args) throws IOException { 

    int total = 0; 

    Scanner in = new Scanner(System.in); 

    System.out.print("Enter word: "); 

    String input = in.nextLine(); 

    // Close the input stream to avoid memory leaks 
    in.close(); 

    if (input == null || input.length() == 0) { 
     System.out.println("Enter input."); 
     System.exit(0); 
    } 

    String inputLower = input.replaceAll("[^a-zA-Z ]", "").toLowerCase(); 

    String inputArray[] = inputLower.split("\\s+"); 

    // Loop through each entered word, I only did this step because you appeared to split up the input even though the Array wasn't actually being used in your original program 
    for(String word : inputArray) { 

     int currentWordTotal = 0; 

     // Use a method to get the value for each letter in the current word 
     for(int i = 0; i < word.length(); i++) { 

      currentWordTotal += letterScore(word.charAt(i)); 

     } 

     // Print out the word total 
     System.out.println("Word: " + word + ", will earn you: " + currentWordTotal + " points."); 

     // Keep track of all word total in the event that multiple words were entered 
     total += currentWordTotal; 

    } 

    // Print out the total for all words 
    System.out.println("Your Total for the entered Words: " + total); 


} 

private static int letterScore(char letter) { 

    char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't','u', 'v', 'w', 'x', 'y', 'z'}; 
    int[] scores = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 9, 1, 1, 1, 1, 4, 4, 8 ,4 ,10}; 

    // Here we look through the array of letters, when we find a match we use the index of the character array to get the corresponding value from the score array 
    for(int i = 0; i < letters.length; i++) { 

     if(letters[i] == letter) { 
      return scores[i]; 
     } 
    } 
    return 0; 
} 
} 
1

您的實際代碼顯示,我將指出主要問題:

  1. 你循環throught alpha.length和你冒着將 轉換爲StringIndexOutOfBoundsException的風險,以便更好地檢查i 是否不大於input.length
  2. 您正在使用substring(i,i+1)獲得位置 我可以取代.charAt(i)這是專爲此目的字符。
  3. 您正在退出該應用程序,如果一個字符中的字母 與它在alpha中的對應字符不匹配,那麼您在檢查 而不檢查輸入的所有字符...

我想糾正你的代碼,這是我想出了結果:

String alpha = "abcdefghijklmnopqrstuvwxyz"; 
    String score = "13321424185131139111144849"; 
    String result = ""; 
    int value = 0, total = 0; 
    Scanner in = new Scanner(System.in); 
    System.out.print("Enter word: "); 
    String input = in.next(); 


    for(int i = 0; i < input.length(); i++) { 

    if(i<alpha.length()) { 
     if(input.charAt(i) == alpha.charAt(i)) 
     { 
      value = Integer.parseInt(score.charAt(i)+""); 
      if(value == 9) 
       value++; 
      result += input.charAt(i) + " will give you " + value + " point(s)\n"; 
      total += value; 
     } 
    } 
    } 
    System.out.println(result + "This word will earn you: " + total); 

這裏是一個Live DEMOalcme作爲輸入,並給出了這樣的結果:

a will give you 1 point(s) 
c will give you 3 point(s) 
e will give you 1 point(s) 
This word will earn you: 5 
+0

做得好上指出他的錯誤。我只是重寫了一些不太有用的代碼 – Richard

相關問題