【问题标题】:Multiple Methods with variables带变量的多种方法
【发布时间】:2018-08-02 21:03:47
【问题描述】:

我需要第一个半径,但它无法识别,我该怎么做?顺便说一句,必须有两种方法,第二种方法必须使用第一种方法的半径。

    public static double areaCircle(double rad) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter radius : ");

        rad = input.nextDouble();

        double circ = Math.PI*Math.pow(rad, 2);

        return circ;

        }

    public static double volumeCylinder (double h) {


        Scanner input = new Scanner(System.in);

        System.out.print("Enter height : ");

        h = input.nextDouble();

        double cyl = Math.PI*Math.pow(rad, 2)*h;
    }

【问题讨论】:

  • 这听起来像是您误解了您的要求。
  • 从函数中取出扫描器并将它们分配给变量。然后将变量传递给函数。在第二个函数中使用第一个函数的返回值。
  • 为什么你有 rad/h 作为方法参数,还把 rad/h 作为输入!做两个之一!

标签: java methods


【解决方案1】:

因为它没有获取有关该变量的信息。你必须传递价值

public static double areaCircle(double rad) {



    double circ = Math.PI*Math.pow(rad, 2);

    return circ;

    }

public static double volumeCylinder (double h, double rad) { //added double rad



    double cyl = Math.PI*Math.pow(rad, 2)*h;
}

当你从 main 调用它时

Scanner input = new Scanner(System.in);
 System.out.print("Enter radius : ");
rad = input.nextDouble();
System.out.print("Enter h: ");
h= input.nextDouble();
double something = volumeCylinder(h,rad);
double something = areaCircle(rad);

【讨论】:

  • 我应该写什么来写一些东西?顺便说一句,它又说,h 和 rad 不能解析为变量。
  • something 是变量的名称,您选择了您想要的名称。您是否在此代码之前声明了 h 和 rad ?双弧度和双小时?
  • 谢谢我解决了,我忘了按你说的声明:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 2019-06-16
相关资源
最近更新 更多