Java 布尔与方法
我是编程新手,对于我的课程,我必须为以下内容编写一个方法和程序:Java 布尔与方法,java,Java,我是编程新手,对于我的课程,我必须为以下内容编写一个方法和程序: boolean allTheSame(双x,双y,双z)返回参数的平均值 public static void main(String[] args) { // **METHOD**// Scanner in = new Scanner(System.in); System.out.println("Enter variable for x"); double x = in.nextDouble
boolean allTheSame(双x,双y,双z)
返回参数的平均值
public static void main(String[] args) {
// **METHOD**//
Scanner in = new Scanner(System.in);
System.out.println("Enter variable for x");
double x = in.nextDouble();
System.out.println("Enter variable for y");
double y = in.nextDouble();
System.out.println("Enter variable for z");
double z = in.nextDouble();
boolean allTheSame = boolean allTheSame(x, y, z);
System.out.println(allTheSame);
}
// **TEST PROGRAM** //
public static boolean allTheSame(double x, double y, double z)
{
if (x == y && x == z)
{
return true;
}
else
{
return false;
}
}
}
既然我是新来的,我该怎么做呢
boolean allTheSame = allTheSame(x, y, z);
System.out.println(allTheSame);
在调用该方法之前,您不需要输入
布尔值。您的具体问题是什么?我知道,我对编程非常陌生,正如您所知,我不太擅长编程。您想知道什么吗?“allTheSame”听起来应该返回一个布尔值,就像您所做的那样。为什么它要返回参数的平均值呢?顺便说一句,任何时候当你有像if(something){return true;}else{return false;}
这样的代码时,你可以只做返回某个东西。它总是一种更好的形式。因此,在您的情况下,只需返回x==y&&x==z
。谢谢!!!!!,现在我的程序是working@FahrenheitNinetyNine伟大的10分钟后(当允许您接受答案时),请单击答案左侧的复选标记接受我的答案。