发出为输入的每个字符分配值并显示输入

时间:2015-05-13 16:26:50

标签: java string loops if-statement for-loop

我正在尝试创建一个接受用户输入的程序,扫描每个子字符串并检查该字符是否与字母表中的位置匹配,如果是,我想获得得分字符串中的等效位置以获得得分该字符并显示每个字符的分数和总分。

我在下面的代码中所拥有的内容应该已经完成​​了我正在尝试做的每一部分,但是第二个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

学习的问题

3 个答案:

答案 0 :(得分:1)

您的实际代码显示了我将要说明的主要问题:

  1. 你正在循环strrev而你正冒着风险去做 进入StringIndexOutOfBoundsException以便更好地检查alpha.length 并不大于i
  2. 您正在使用input.length来获取该位置的角色 我可以用substring(i,i+1)替换为此而设计的。
  3. 如果某个字符在agiven中,您将退出该应用程序 并不匹配其在alpha中的相应的一个,所以你要退出 没有检查输入的所有字符......
  4. 我尝试纠正您的代码,这是我提出的结果:

    .charAt(i)

    这里有Live DEMO 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); 作为输入并给出了这个结果:

    alcme

答案 1 :(得分: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,并使用该索引查找分数数组。

答案 2 :(得分: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;
}
}