You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java单词字符积分计算功能实现求助

Solution for Your Java Points Problem

Hey there! Let's get your points calculation feature up and running—since you're new to Java, I'll break this down clearly so you can follow along.

First, let's outline what we need to implement in the pointsProblemGame() method:

  • Get a word input from the user
  • Loop through each character in that word
  • Look up the character's value from your pre-defined valueOfCharacter HashMap
  • Add all those values together to get the total points for the word
  • Display the final result

Here's how to fill in the pointsProblemGame() method with all that logic:

static void pointsProblemGame() throws FileNotFoundException {
    System.out.println("Points problem.");
    Scanner pointsInput = new Scanner(System.in);
    System.out.print("Enter a word to calculate its points: ");
    String inputWord = pointsInput.nextLine().toLowerCase(); // Convert to lowercase to match HashMap keys
    
    int totalPoints = 0;
    
    // Loop through each character in the input word
    for (char c : inputWord.toCharArray()) {
        // Check if the character exists in our HashMap to avoid null errors
        if (valueOfCharacter.containsKey(c)) {
            totalPoints += valueOfCharacter.get(c);
        }
        // Optional: Add handling for characters without point values (like symbols/numbers)
        // else {
        //     System.out.println("Character '" + c + "' has no assigned point value.");
        // }
    }
    
    System.out.println("Total points for '" + inputWord + "' is: " + totalPoints);
    System.out.println();
    gameMenuSelection(); // Return to the main menu after calculation
}

Key Details to Understand:

  • toLowerCase(): Your HashMap uses lowercase characters as keys, so converting the user's input to lowercase ensures we don't miss matches (e.g., if the user types "Banana", it becomes "banana" and each character is found correctly).
  • toCharArray(): Converts the input string into an array of characters, making it easy to loop through each one with a clean enhanced for loop.
  • containsKey(c): A quick safety check to make sure the character exists in our map—this prevents a NullPointerException if the user enters a character you haven't defined (like a number or symbol).
  • totalPoints += valueOfCharacter.get(c): This shorthand syntax adds each character's point value to our running total, replacing the longer totalPoints = totalPoints + valueOfCharacter.get(c) line.

Quick Side Suggestion:

I noticed your randomSubstringResults array is set to a fixed size of 23. If your dictionary.txt has more than 23 words, this will cause an ArrayIndexOutOfBoundsException. A better approach would be to use an ArrayList<String> instead of a fixed array, since it can grow dynamically as you read words from the file. But that's a separate tweak—focus on getting the points feature working first!

Give this code a try, and let me know if you run into any other bumps along the way. 😊

内容的提问来源于stack exchange,提问作者wallace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 23:02:34