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

如何返回基于输入方法计算出的超速罚单判定结果?

How to Return Your Speeding Ticket Calculation Result in Java

It sounds like you’ve got the core logic down for checking speeds and determining violations—great start! The key here is to wrap that IF statement logic into a dedicated method that returns your calculation result, which you can then use anywhere (including your future GUI). Here’s how to do it step by step:

Step 1: Extract Calculation Logic into a Reusable Method

Instead of keeping the IF logic directly in your main method (or wherever it’s currently located), move it into a method with a clear return type. This makes your code cleaner and lets you easily retrieve the result whenever you need it.

For example, if you want to return a human-readable message about the ticket, use a String return type. If you need more details (like fine amount and violation level), you can even create a custom class to hold all that data (we’ll cover that too).

Example Implementation

Here’s how to refactor your existing code to return the result:

import java.util.Scanner;

public class SpeedingTicketClass {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        // Get input values (you'll replace these with GUI input later)
        System.out.print("Enter the speed limit: ");
        int speedLimit = scan.nextInt();
        
        System.out.print("Enter the vehicle's speed: ");
        int vehicleSpeed = scan.nextInt();
        
        // Call our calculation method and store the result
        String ticketResult = calculateSpeedingTicket(speedLimit, vehicleSpeed);
        
        // For now, print the result—later you'll display this in your GUI
        System.out.println("Ticket Result: " + ticketResult);
        
        scan.close();
    }
    
    // This method takes speed limit and vehicle speed, returns the ticket outcome
    private static String calculateSpeedingTicket(int speedLimit, int vehicleSpeed) {
        if (vehicleSpeed <= speedLimit) {
            return "No ticket issued. You were driving within the speed limit.";
        } else {
            int overSpeed = vehicleSpeed - speedLimit;
            if (overSpeed <= 10) {
                return "Minor violation: $100 fine.";
            } else if (overSpeed <= 20) {
                return "Moderate violation: $250 fine.";
            } else {
                return "Major violation: $500 fine + mandatory court appearance.";
            }
        }
    }
}

Key Notes:

  • Return Statements: Each branch of your IF logic uses a return statement to send back the specific result for that scenario. This ensures every possible path in the method returns a value.
  • Reusability: Now you can call calculateSpeedingTicket() from anywhere in your code—once you build your GUI, you’ll pass the speed values from your input fields to this method, then display the returned string in a label or text area.

If You Need More Detailed Results

If you want to return multiple pieces of data (like the fine amount as a number, not just a string), create a custom class to hold those values:

// Custom class to store ticket details
class SpeedingTicket {
    private int fineAmount;
    private String violationLevel;
    private String message;
    
    // Constructor
    public SpeedingTicket(int fineAmount, String violationLevel, String message) {
        this.fineAmount = fineAmount;
        this.violationLevel = violationLevel;
        this.message = message;
    }
    
    // Getters to access the data
    public int getFineAmount() { return fineAmount; }
    public String getViolationLevel() { return violationLevel; }
    public String getMessage() { return message; }
}

Then modify your calculation method to return an instance of this class:

private static SpeedingTicket calculateSpeedingTicket(int speedLimit, int vehicleSpeed) {
    if (vehicleSpeed <= speedLimit) {
        return new SpeedingTicket(0, "None", "No ticket issued.");
    } else {
        int overSpeed = vehicleSpeed - speedLimit;
        if (overSpeed <= 10) {
            return new SpeedingTicket(100, "Minor", "Minor violation: $100 fine.");
        } else if (overSpeed <= 20) {
            return new SpeedingTicket(250, "Moderate", "Moderate violation: $250 fine.");
        } else {
            return new SpeedingTicket(500, "Major", "Major violation: $500 fine + court appearance.");
        }
    }
}

This way, you can retrieve individual pieces of data (like the fine amount) to use in your GUI (e.g., displaying it in a dedicated field).

Once you have this method in place, integrating it with your future GUI will be straightforward—just grab the input values from your GUI components, call the method, and use the returned result to update your UI.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:18:58