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

图书逾期罚款计算程序重复询问图书数量问题排查及修复需求

Fixing Your Book Overdue Fine Program (Duplicate Input & Logic Issues)

Let’s start by breaking down why your program is asking for the number of books multiple times, then fix it up to work as intended—plus add some robustness for edge cases like invalid inputs.

What’s Causing the Duplicate Prompts?

  1. Redundant method calls in ConvertFine: Your ConvertFine function calls GetBook(book) again, which triggers the "Enter the number of books" prompt a second time after you input the days. That’s the biggest issue.
  2. Unnecessary GetBook call in Main: You already ask for the book count in Main, then immediately call GetBook(book) and print it—this adds an extra prompt if the initial input was over 5, or just a useless print if it was valid.
  3. Pointless GetDays method: This method does nothing but return the input value—it’s completely redundant and doesn’t help with validation.

Fixed Code with Explanations

Here’s a revised version that fixes all the duplicate input issues, adds proper validation, and follows the rules you outlined:

using System;

class Program
{
    // Handles getting and validating the number of books (no more duplicate prompts!)
    public static int GetValidBookCount()
    {
        int bookCount;
        bool isValid = false;

        do
        {
            Console.WriteLine("Enter the number of books: ");
            // Try to parse input to avoid crashes from non-numeric values
            if (int.TryParse(Console.ReadLine(), out bookCount))
            {
                if (bookCount > 5 || bookCount < 1) // Ensure valid range (1-5 books)
                {
                    Console.WriteLine("Invalid value! Max number of books is 5, minimum is 1.");
                }
                else
                {
                    isValid = true;
                }
            }
            else
            {
                Console.WriteLine("Please enter a valid number.");
            }
        } while (!isValid);

        return bookCount;
    }

    // Gets and validates overdue days (handles negative days and non-numeric input)
    public static int GetValidOverdueDays()
    {
        int overdueDays;
        bool isValid = false;

        do
        {
            Console.WriteLine("Enter the number of overdue days: ");
            if (int.TryParse(Console.ReadLine(), out overdueDays))
            {
                if (overdueDays < 0) // Can't have negative overdue days
                {
                    Console.WriteLine("Invalid value! Days can't be negative.");
                }
                else
                {
                    isValid = true;
                }
            }
            else
            {
                Console.WriteLine("Please enter a valid number.");
            }
        } while (!isValid);

        return overdueDays;
    }

    // Calculates fine based on your rules, no redundant input calls
    public static double CalculateOverdueFine(int days, int bookCount)
    {
        double fine = 0;

        if (days <= 21)
        {
            Console.WriteLine("Fine is: 0 euros");
        }
        else if (days >= 22 && days <= 30)
        {
            fine = (days - 21) * 0.5 * bookCount;
            Console.WriteLine($"Fine is: {fine:F2} euros"); // Formats to 2 decimal places for currency
        }
        else // Days over 30
        {
            fine = (days - 21) * 0.8 * bookCount;
            Console.WriteLine("Membership cancelled!");
            Console.WriteLine($"Fine is: {fine:F2} euros");
        }

        return fine;
    }

    static void Main(string[] args)
    {
        // Get valid inputs ONCE, then pass them to the calculation method
        int bookCount = GetValidBookCount();
        int overdueDays = GetValidOverdueDays();

        CalculateOverdueFine(overdueDays, bookCount);
    }
}

Key Changes Made:

  • No more duplicate prompts: We get valid book count and overdue days exactly once in Main, then pass those values to the calculation method—no repeated calls to input functions.
  • Robust input validation: Added checks for non-numeric inputs (so the program won’t crash if someone types "abc") and valid ranges (1-5 books, no negative days).
  • Removed useless methods: Got rid of the redundant GetDays method and replaced it with a proper input validation function for days.
  • Cleaner output: Used string interpolation and F2 formatting to display the fine with 2 decimal places, which is standard for currency.
  • Clearer logic flow: The program now follows a straightforward path: get inputs → calculate fine → display results.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 18:28:04