代码之家  ›  专栏  ›  技术社区  ›  BigO

单词积分游戏不断增加数字[关闭]

  •  0
  • BigO  · 技术社区  · 2 年前
    public static void pointsProblem() throws java.io.FileNotFoundException {
        FileInputStream pp = new FileInputStream("dictionary.txt");
        Scanner dictionaryIn = new Scanner(pp);
        Scanner input = new Scanner (System.in);
        System.out.println("Points Problem.");
        
        char[] alphabet = {'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[] points = { 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 score = 0;
        String word; 
        while (dictionaryIn.hasNextLine()) { 
            word = dictionaryIn.nextLine();
            for (int i = 0; i < word.length(); i++) {
                for (int n = 0; n < 26; n++) {
                    if (word.charAt(i) == alphabet[n]) { 
                    score += points[n];
                }
                }
            }
            System.out.println(word + "is worth " + score + " points.");
        }
        System.out.println("");
    }
    

    在我的积分游戏中,我有一个文本文件,上面有文字。每次我运行游戏时,所有单词的分数都会加到下一个单词中。我希望每个单词都从0开始,而不是从前面单词的指定点开始。

    这是输出

    点问题。 路人价值16分。

    绝对值得44分。

    巴纳纳值56分。

    这值62分。

    quickis值82分。

    布朗尼值92分。

    Fox值105分。

    它值128分。

    这值134分。

    懒汉值150分。

    狗值155分。

    人工智能价值156点。

    mmis值162分。

    WOWI值171分。

    noonis值175分。

    拉达里斯值181分。

    redderis值189分。

    racecaris值200分。

    雷迪维德里斯值214分。

    塔塔拉塔提斯值249分。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Blunt Jackson    2 年前

    放松点!

    移动

    int score = 0;
    

    在你的while循环中,这样它每次都会被重置!