简体   繁体   English

为什么我的代码没有打印出新的气球体积?

[英]Why is my code not printing out the new balloon volume?

I'm a beginner at Java and I'm having trouble understanding why my "Inflate" and "getVolume" methods aren't working.我是 Java 的初学者,我无法理解为什么我的“Inflate”和“getVolume”方法不起作用。 I'm sure they're just simple problems but I'd still like some help so I can fix my code and improve!我确定它们只是简单的问题,但我仍然需要一些帮助,以便我可以修复我的代码并改进!

import java.util.Scanner;

public class Balloon
{
    public static void main(String[] args)
    {
        Scanner multiplier = new Scanner(System.in);
        System.out.println("How much should the radius be increased by? ");

        double amount = multiplier.nextDouble();

        double radius = 0;

        public void Inflate(double amount);
        {       
            double newRadius = radius + amount;
        }
        public double getVolume();
        {
            double sVolume = (4/3)*Math.PI*(newRadius*newRadius*newRadius);
            System.out.print(sVolume);
        }
    }
}

I suppose that Ballon is an object you can inflate and has an state of radius, you can moreover get the volume.我想 Ballon 是一个可以膨胀的物体,并且具有半径状态,此外还可以获得体积。

The main method here is only to test if balloon works correctly这里的主要方法只是测试气球是否正常工作

public class Balloon {

private double radius = 0;

public static void main(String[] args) {
    Scanner multiplier = new Scanner(System.in);
    System.out.println("How much should the radius be increased by? ");

    Balloon balloon=new Balloon();
    double amount = multiplier.nextDouble();
    balloon.inflate(amount);
    double volume = balloon.getVolume();
    System.out.print(volume);
}

public void inflate(double amount) {
    radius = radius + amount;
}

public double getVolume() {
    double sVolume = (4 / 3) * Math.PI * (Math.pow(radius, 3));
    return sVolume;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM