我试图完成这个Java项目几天来一直在破坏我的大脑,而在我的生活中,我无法让它运行起来。这是我应该做的:
编写一个程序,从下面的列表中调用Math类的每个方法,并提供显示调用方法,发送给方法的值和返回结果的输出。每个列出的方法都会告诉您使用哪些值。例如:
列出的方法:double pow(double a, double b):
使用2.0
和3.0
。您的程序将显示:Math.pow(2.0, 3.0) = 8.0
。当调用接受双精度的方法时,使用至少一个十进制数字的数字,如上例所示,即使它是零。请记住,带小数的数字是双字面值。尝试使用可产生易于验证的结果的值。
以下是清单:
double pow(double a, double b): Use 3.0 and 2.0 double sqrt(double x): Use 25.0 int max(int a, int b): Use 6 and 2 double max(double a, double b): Use -50.0 and 7.0 static double random()
示例输出:
Math.pow(3.0, 2.0) = 9.0 Math.sqrt(25.0)=5.0 Math.max(6,2)=6 Math.max(-50.0,7.0)=7.0 Math.random()= -random value-
使用示例中显示的值测试您的程序,修复所有错误。
在您的程序中添加一个名为randomStudy的方法,该方法没有参数并且不返回任何值。在此方法中,执行以下操作:
一个。声明三个int变量:total,min和max。将总数设置为0,将min设置为11,将max设置为-1。
湾创建一个运行1,000次的循环。在循环体中,生成1到10之间的随机int值,包括1和10。将此数字添加到您的总数中。如果此数字小于min,则使用新数字更新min。如果它大于max,则使用新号码更新max。
℃。循环后,显示以下内容:
Min value: x Max value: y Average: z
用最小值和最大值替换x和y。通过将总数除以1000d来计算z。
从main方法调用新的randomStudy方法。
突出显示并复制Eclipse中显示的输出。将输出粘贴到源代码文件的底部。在结果上方添加文本结果:然后注释掉刚刚添加的文本加上输出文本。您评论的结果应如下所示:
/* Results: Math.pow(3.0, 2.0)= 9.0 Math.sqrt(25.0) = 5.0 Math.max(6, 2) = 6 Math.max(-50.0, 7.0) = 7.0 Math.random() = -random number- Min value: 1 Max value: 10 Average: 5.553 */
public class Method {
public void main(String[] args) {
public static double pow(double 3.0, double 2.0);
public static double sqrt(double 25);
public static int max(int 6, int 2);
public static double max(double -50, double 7)
public static double random()
}
}
public class randomStudy{
public void main(String[] args) {
int min = 11;
int max = -1;
int total = 0;
int i;
for(i = 0; i < 1000; i++){
int randomInt = (int)(11.0 * Math.random());
System.out.println("Random integer between 1 and 10 : " + randomInt );
int newtotal = (randomInt + int total);
if int newtotal < int min {
int min = Math.random());
}
else{
System.out.println("int min");
}
if int newtotal > max {
int max = Math.random());
}
else{
System.out.println("int max");
}
}
System.out.println("Min value:" + int min);
System.out.println("Max Value:" + int max);
System.out.println("Average:"+ int newtotal / 1000d);
}
}
我收到此错误
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method pow(double, double) is undefined for the type Math
The method pow(double, double) is undefined for the type Math
at Math.main(Math.java:3)
答案 0 :(得分:0)
您的代码结构不正确。你有多个主要方法,这是一个大问题。特别是这个程序应该只有一个。您正在声明已存在的变量:
int variableA = 0;
int variableA; // Overrides old variableA thus resetting the value
相反,您应该这样做以使用变量:
int variableA = 0;
variableA = 10;
您也在主方法中声明了不能做的方法。这也是导致错误的原因。
public static double pow(double 3.0, double 2.0);
public static double sqrt(double 25);
public static int max(int 6, int 2);
public static double max(double -50, double 7)
public static double random()
当你声明一个数据类型如'double'时,你也不能写'double 3.0'你接下来要写的是变量名,而不是值。在尝试继续之前,请仔细阅读Java和编程基础知识。
您将在下面找到修复并重新格式化的代码。但是我不是在为你工作,所以你必须弄清楚如何自己完成并通过实例学习。
在'Math.java':
public class Math {
public static void main(String[] args) {
// Program starts here
// Show and calculate math results here
System.out.println("Math.pow(3.0, 2.0) = " + Math.pow(3.0, 2.0));
// ...
// This will execute the randomStudy method.
randomStudy();
}
public void randomStudy() {
// randomStudy method code goes here
int min = 11;
int max = -1;
int total = 0;
for(int i = 0; i < 1000; i++) {
int randomInt = (int)(11.0 * Math.random());
System.out.println("Random integer between 1 and 10: " + randomInt);
int newtotal = (randomInt + total);
if (newtotal < min) {
min = Math.random());
} else {
System.out.println("Min: " + min);
}
if (newtotal > max) {
max = Math.random());
} else {
System.out.println("Max: " + max);
}
}
System.out.println("Min value:" + min);
System.out.println("Max Value:" + max);
System.out.println("Average:"+ newtotal / 1000d);
}
}