1

我的代码编译,但输出不会显示。我认为我的方法都是正确的,但我不知道为什么我的输出不会显示。(注意: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);
    }
}
4

5 回答 5

0

尝试在您的主要方法中调用您的计算方法之一,发布输入,例如:

System.out.println(Lab8.Calculations(radius,height));
于 2013-11-09T05:53:20.130 回答
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)));
}
于 2013-11-09T05:54:48.247 回答
0

您没有在main(...). 尝试添加对main 函数outputcalculations在 main 函数中的调用。

特别是,像

output(Calculations(radius, height), calculations(radius, height));

可能就足够了。

在您学习 Java 时,开始将描述性名称应用于您的函数可能会有所帮助,例如CylinderSurfaceArea帮助您和任何可能阅读您编写的代码的人将混淆降至最低。

于 2013-11-09T05:51:19.837 回答
0

你实际上并没有调用你的方法。

main, 在你得到radiusand之后height, 分别调用Calculationsand calculations, 并带有适当的参数。

这是一个例子:

surfaceArea = Calculations(radius, height);
volume = calculations(radius, height);
于 2013-11-09T05:51:27.417 回答
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);
    }
      }
于 2013-11-09T05:52:10.533 回答