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

Java程序中Height(int, int)方法参数不匹配导致Run()报错的问题求助及代码修正咨询

Hey there! Let's break down what's going wrong with your code and fix it step by step.

First, let's unpack the error you're seeing

The error The method Height(int, int ) is not applicable for arguments () pops up because:

  • You defined the Height() method to require two parameters (length, width), but in your Run() method you're calling it without passing any arguments (Height()).
  • Even if you did pass arguments, that method's definition is invalid in Java—you didn't specify data types for the parameters (it should be int length, int width), and that method shouldn't even need those parameters anyway (it's just for collecting height input!).

Other critical issues breaking your program

Let's list out all the hidden problems:

  1. Invalid method parameter declaration: public static int Height(length, width) is illegal in Java—you must declare the type of each parameter.
  2. Unreachable code: In the Height() method, you have return height; before all the volume calculation code. Once a return runs, nothing after it executes—so your balloon input and volume math will never run.
  3. Undefined variable: In the Length() method, you use length without declaring its type (int length = scanner.nextInt(); is required).
  4. Poor method responsibility: Your Height() method is trying to do too much—get height input, get balloon volume, calculate room volume, and output results. Methods should focus on one task each.
  5. Redundant Scanner instances: You create a new Scanner in every input method. You only need one Scanner for the entire program.
  6. Integer truncation: When you cast BalloonAmount to int, you're losing decimal values. Since you can't use a fraction of a balloon, you should round up instead.

Fixed Working Code

Here's a cleaned-up, functional version with explanations in comments:

import java.util.Scanner;

// Follow Java naming conventions (class names in PascalCase)
class RoomBalloonCalculator {
    // Use one Scanner instance for all input to avoid resource waste
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        runCalculation();
        scanner.close(); // Clean up the scanner when done
        System.exit(0);
    }

    // Show input instructions once instead of repeating them
    private static void showInputGuide() {
        System.out.println("Enter values as whole numbers (no decimals, no units).");
        System.out.println("Good example: 5, 7, 9 | Bad example: 3.2, 5cm\n");
    }

    // Get room length (single job: just collect length input)
    public static int getRoomLength() {
        showInputGuide();
        System.out.print("Length of room (in cm): ");
        return scanner.nextInt();
    }

    // Get room width
    public static int getRoomWidth() {
        System.out.print("Width of room (in cm): ");
        return scanner.nextInt();
    }

    // Get room height
    public static int getRoomHeight() {
        System.out.print("Height of room (in cm): ");
        return scanner.nextInt();
    }

    // Get balloon volume
    public static float getBalloonVolume() {
        System.out.print("\nEnter the volume of one balloon (in cubic meters): ");
        return scanner.nextFloat();
    }

    // Calculate and display results in one dedicated method
    private static void calculateAndShowResults(int length, int width, int height, float balloonVolume) {
        // Calculate room volume in cm³, then convert to m³ (1 m³ = 1,000,000 cm³)
        double roomVolumeCm3 = length * width * height;
        double roomVolumeM3 = roomVolumeCm3 / 1000000.0; // Use 1000000.0 to avoid integer division

        // Calculate balloons needed, round up since partial balloons don't work
        double balloonsNeeded = roomVolumeM3 / balloonVolume;
        int totalBalloons = (int) Math.ceil(balloonsNeeded);

        // Print formatted results for readability
        System.out.printf("\nThe volume of the room is: %.2f cubic meters\n", roomVolumeM3);
        System.out.printf("Number of balloons needed to fill the room: %d\n", totalBalloons);
    }

    // Main workflow to tie everything together
    public static void runCalculation() {
        int length = getRoomLength();
        int width = getRoomWidth();
        int height = getRoomHeight();
        float balloonVolume = getBalloonVolume();

        calculateAndShowResults(length, width, height, balloonVolume);
    }
}

Key improvements made:

  • Followed Java naming conventions for readability
  • Split code into small, single-responsibility methods (easier to debug and modify)
  • Used one Scanner instance and closed it properly
  • Fixed unit conversion to avoid integer division errors
  • Used Math.ceil() to round up balloon count (you can't use half a balloon!)
  • Added clean formatted output with printf()
  • Removed redundant code (input instructions only print once)

How to run:

  1. Compile with javac RoomBalloonCalculator.java
  2. Run with java RoomBalloonCalculator
  3. Follow the prompts to enter your values

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 20:07:51