我的代码编译,但输出不会显示。我认为我的方法都是正确的,但我不知道为什么我的输出不会显示。 (注意:Java初学者)
import java.util.Scanner;
import java.lang.Math;
public class Lab8 {
public static void main(String[] args) {
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius = in.nextDouble();
System.out.print("Enter height: ");
height = in.nextDouble();
}
public static double Calculations(double radius, double height) {
double surfaceArea = (2 * Math.PI * radius * radius) + (2 * Math.PI * radius * height);
return surfaceArea;
}
public static double calculations(double radius, double height) {
double volume = Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume) {
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
答案 0 :(得分:0)
你没有在main(...)
内调用任何帮助函数。尝试在主要功能中添加对output
或calculations
的通话。
特别喜欢
output(Calculations(radius, height), calculations(radius, height));
可能就足够了。
在学习Java时,开始在函数中应用描述性名称可能会有所帮助,例如CylinderSurfaceArea
可以帮助您和任何可能阅读您编写的代码的人保持最低限度的混淆。< / p>
答案 1 :(得分:0)
您实际上并没有调用您的方法。
在main
中,获得radius
和height
后,分别使用适当的参数调用Calculations
和calculations
。
以下是一个例子:
surfaceArea = Calculations(radius, height);
volume = calculations(radius, height);
答案 2 :(得分:0)
您需要在main中调用您的方法来打印值。
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
固定代码:
import java.util.Scanner;
import java.lang.Math;
public class Lab8
{
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
}
public static double Calculations(double radius, double height)
{
double surfaceArea= (2 * Math.PI * radius * radius) + (2 * Math.PI *radius* height);
return surfaceArea;
}
public static double calculations(double radius,double height)
{
double volume= Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume)
{
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
答案 3 :(得分:0)
尝试在主方法中调用一个Calculations方法,然后输入如下输入:
System.out.println(Lab8.Calculations(radius,height));
答案 4 :(得分:0)
在main中你错过了计算和输出方法调用
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
// call this
System.out.println("area " + output(Calculations(radius,height)));
}