我迫切需要一个Java程序的帮助,我需要竞争学校!在过去的三天里,我已经在这方面工作了至少三个小时,我想不出来=(以下是说明:
编写一个程序,从下面的列表中调用Math类的每个方法,并提供输出,显示调用的方法、发送给该方法的值以及返回的结果。列出的每个方法都会告诉您要使用哪些值。例如:
列出的方法:
double pow(double a, double b): use 2.0 and 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-*/
用示例中显示的值测试您的程序,修复任何错误。向程序中添加一个名为随机研究的方法,该方法没有参数,也没有返回值。在此方法中,执行以下操作:
a、 声明三个int变量:total、min和max。将total设置为0,min设置为11,max设置为-1。
b、 创建一个将运行1000次的循环。在循环体中,生成一个介于1和10之间的随机int值。把这个数字加到你的总数上。如果此数字小于min,请使用新数字更新min。如果大于最大值,则用新数字更新最大值。
c、 循环结束后,显示以下内容:
最小值:x最大值:y平均值:z将x和y替换为最小值和最大值。计算z除以你的总数1000d。从主方法调用新的随机研究方法。高亮显示并复制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
*/
所以,我遇到的问题是最后一部分关于运行循环1000次,然后显示1000个数字中最大和最小的数字。并显示1000个数字的平均值。这是我的代码:
class MathMethodsProgram {
public static void main(String[] args) {
// Prints the pow method.
System.out.println("Math.pow(2.0, 3.0) = " + Math.pow(2.0, 3.0));
// Prints the sqrt method
System.out.println("Math.sqrt(25) = " + Math.sqrt(25));
// Prints the max number
System.out.println("Math.max( 6, 3) = " + Math.max(6, 3));
// Prints the max number
System.out.println("Math.max(-50.0,7.0) = " + Math.max(-50.0, 7.0));
// Prints a random number
System.out.println("Math.random(); = " + Math.random());
randomStudy();
}
public static void randomStudy(){
int total = 0;
int min = 11;
int max = -1;
int newtotal = 0;
int i;
int randomInt = 0;
for( i = 0; i < 1000; i++){
randomInt = 1 + (int)( Math.random() * 11);
newtotal = (randomInt + total);
}
if (randomInt < min) {
min = 1 + (int) Math.random();
} else {
System.out.println("Min: " + min);
}
if (randomInt > max) {
max = 1 + (int) 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);
}
}
当我在Eclipse中运行代码时,这是输出:
Math.pow(2.0, 3.0) = 8.0
Math.sqrt(25) = 5.0
Math.max( 6, 3) = 6
Math.max(-50.0,7.0) = 7.0
Math.random(); = 0.1824716748725944
Min value:1
Max Value:1
Average:0.008
提前谢谢
在这里,我将提供我创建的代码。我的目的是为您提供如何创建与我们从Math类中获得的函数等价的多个函数的想法。我包含了帮助这些类不言自明的注释,我希望这对你有所帮助。我还包括对话框来显示数据,所以如果有人正在寻找如何使用类javax.swing帮助,这是一个很好的资源。
第一类MyMath.java包含用于执行所有计算的方法。
package com.exercise.lecture2;
/**
* Create a class call "MyMath" that has the following class methods:
* static boolean isEven(int x) --> returns true is number is even
* static long absolute(long x) --> returns the absolute value of x
* static double hypotenuse (double x, double y) --> returns sqrt(x^2 + y^2)
* static double max (double a, double b) --> returns max of a or b
* static double min (double a, double b ) --> returns min of a or b
*
* @author Samuel
*
*/
public class MyMath {
/**
* An even number is an integer which is "evenly divisible" by two.
* This means that if the integer is divided by 2, it yields no remainder.
* Zero is an even number because zero divided by two equals zero, which despite not being a natural number, is an integer.
* Even numbers can be either positive or negative.
*
* @param x
* @return
*/
static boolean isEven(int x){
if (x % 2 == 0)
return true;
else
return false;
}
/**
* In mathematics, the absolute value or modulus of a real number is the number without the sign.
* The absolute value of 2 is 2, the absolute value of -2 is also 2.
*
* @param x
* @return
*/
static long absolute(long x) {
if (x >= 0)
return x;
else
return (-1*x);
}
/**
* The hypotenuse is the side opposite the 90 degrees angle in a right triangle. It is always the longest side.
*
* @param x
* @param y
* @return
*/
static double hypotenuse (double x, double y) {
return Math.sqrt((x * x) + (y * y));
}
/**
* Maximum of two values
* @param a
* @param b
* @return
*/
static double max (double a, double b) {
if (a >= b)
return a;
else
return b;
}
/**
* Minimum of two values
* @param a
* @param b
* @return
*/
static double min (double a, double b ) {
if (a < b)
return a;
else
return b;
}
}
二级数学课堂测试。java包含javax。swing类,以便使用对话框显示。
package com.exercise.lecture2;
import javax.swing.JOptionPane; // importing the javax.swing class in order to use dialog box in my code
/**
* static boolean isEven(int x) --> returns true is number is even
* static long absolute(long x) --> returns the absolute value of x
* static double hypotenuse (double x, double y) --> returns sqrt(x^2 + y^2)
* static double max (double a, double b) --> returns max of a or b
* static double min (double a, double b ) --> returns min of a or b
*
* Write an application that allows the user to select between the above methods and
* returns the response to the user.
*
* @author Samuel
*
*/
public class MathClassTest {
public static void main(String[] args) {
/* used to test the functions on MyMath.java class, here is how all of this begin
* then I change it to make this tester use javax.swing class in order to display using
* dialog boxes.
*
// is Even
System.out.println(MyMath.isEven(-100));
// Absolute value of an integer
System.out.println(MyMath.absolute(-10));
// Hypotenuse of two values
System.out.println(MyMath.hypotenuse(3, 4));
// Max of two values
System.out.println(MyMath.max(3, -4));
// Min of two values
System.out.println(MyMath.min(3, -4));
*/
boolean repeat;
do {
//String name = JOptionPane.showInputDialog(null, "Enter your name", "Enter name", JOptionPane.PLAIN_MESSAGE);
String option = JOptionPane.showInputDialog(null,"Choose 'a' or 'A' to determine if a number is EVEN or not. \n" // options
+ "Choose 'b' or 'B' to determine the ABSOLUTE value of a number. \n" // the (+) sign concatenate the all of the followings Strings as one
+ "Choose 'c' or 'C' to determine the HYPOTENUSE of two values. \n"
+ "Choose 'd' or 'D' to determine the MAXIMUM of two values. \n"
+ "Choose 'e' or 'E' to determine the MINIMUM of two values. \n"
+ "Choose 'q' or 'Q' to quit this simple application. \n\n"
, "My Math class, by S. Mayol", JOptionPane.QUESTION_MESSAGE); // title bar of the dialog box
repeat = true;
if (option.equals("Q") || option.equals("q")) {
JOptionPane.showMessageDialog(null, "Thank you for participating...", "by Samuel Mayol", JOptionPane.INFORMATION_MESSAGE);
repeat = false;
System.exit(0);
} else {
if (option.equals("A")||option.equals("a")){
try { // to prevent that the value inserted is an integer and not a letter
String val = JOptionPane.showInputDialog("Enter a number to deternine if the number is even");
JOptionPane.showMessageDialog(null, "Is the number " + val + " even? " + MyMath.isEven(Integer.valueOf(val)),
"Even", JOptionPane.INFORMATION_MESSAGE); // add a title message on a dialog box
repeat = true;
} catch(Exception e){
JOptionPane.showMessageDialog(null, "The input " + option + " is not valid integer, please try again", "WARNING!!!",
JOptionPane.WARNING_MESSAGE);
}
}
else if (option.equals("B")||option.equals("b")){
try {
String val = JOptionPane.showInputDialog("Enter a number to calculate absolute value of that number");
JOptionPane.showMessageDialog(null, "The absolute value of " + val + " is: " + MyMath.absolute(Long.valueOf(val)),
"Absolute", JOptionPane.INFORMATION_MESSAGE);
repeat = true;
} catch(Exception e){
JOptionPane.showMessageDialog(null, "The input " + option + " is not valid integer, please try again", "WARNING!!!",
JOptionPane.WARNING_MESSAGE);
}
}
else if (option.equals("C")||option.equals("c")){
try {
String val = JOptionPane.showInputDialog("Enter the first side to calculate the hypotenuse");
String val2 = JOptionPane.showInputDialog("Enter the second side to calculate the hypotenuse");
JOptionPane.showMessageDialog(null, "The hypotenuse of " + val + " and " + val2 + " is: " + MyMath.hypotenuse(Double.valueOf(val), Double.valueOf(val2)),
"Hypotenuse", JOptionPane.INFORMATION_MESSAGE);
repeat = true;
} catch(Exception e){
JOptionPane.showMessageDialog(null, "The any of the inputs " + option + " are not valid integer, please try again", "WARNING!!!",
JOptionPane.WARNING_MESSAGE);
}
}
else if (option.equals("D")||option.equals("d")){
try {
String val = JOptionPane.showInputDialog("Enter the first number to calculate the max value");
String val2 = JOptionPane.showInputDialog("Enter the second number to calculate the max value");
JOptionPane.showMessageDialog(null, "The max of " + val + " and " + val2 + " is: " + MyMath.max(Double.valueOf(val), Double.valueOf(val2)),
"Maximum", JOptionPane.INFORMATION_MESSAGE);
repeat = true;
} catch(Exception e){
JOptionPane.showMessageDialog(null, "The any of the inputs " + option + " are not valid integer, please try again", "WARNING!!!",
JOptionPane.WARNING_MESSAGE);
}
}
else if (option.equals("E")||option.equals("e")){
try {
String val = JOptionPane.showInputDialog("Enter the first number to calculate the min value");
String val2 = JOptionPane.showInputDialog("Enter the second number to calculate the min value");
JOptionPane.showMessageDialog(null, "The min of " + val + " and " + val2 + " is: " + MyMath.min(Double.valueOf(val), Double.valueOf(val2)),
"Minimum", JOptionPane.INFORMATION_MESSAGE);
repeat = true;
} catch(Exception e){
JOptionPane.showMessageDialog(null, "The any of the inputs " + option + " are not valid integer, please try again", "WARNING!!!",
JOptionPane.WARNING_MESSAGE);
}
}
else
JOptionPane.showMessageDialog(null, "The input " + option + " is not valid, please try again", "A, B, C, D, E, or Q",
JOptionPane.WARNING_MESSAGE);
}
} while(repeat);
}
}
你的代码非常接近。
你只需要把你的if语句放在for循环和你写的地方
max = 1 + (int) Math.random();
应改为:
max = randomInt;
(对min执行相同操作,使其显示为“min=randomInt;”)
在你的1000 for循环中,你有
newtotal = (randomInt + total);
它不会增加新总数,只会取最后一个数字。看起来你的上一个数字是8,所以平均值是0.008。最好是:
total = (randomInt + total);
// Then after the for loop is complete, average = total / 1000;
接下来,为了比较randomInt和min/max,而不是分配一个新的随机值,您应该基于最后一个随机数分配新的min/max(这需要在1000 for循环内,而不是之后):
if (randomInt > max) {
max = randomInt;
}
希望这能有所帮助!
我真的很想了解,谢谢你的帮助。我一直在看Derek Banas Java编程视频,我被以下问题困扰: int randomNumber=(int)(Math.random()*126)1; 目标是获得从1到126包括的随机数。它的工作,但为什么?它怎么能输出126呢? > 如果铸造int删除小数。 126包括125(见测试) If Math.random()返回0-0.9(含0.9)。 最接近的是
如果我做:Math.random() * 4-2 这会让我得到一个范围(-2,2),2是排他性的吗?我认为这是正确的,但我很少得到正数(是的,我知道这是一个随机算法,我们必须无限随机地生成它才能感觉到,但我只是想确保) 新问题 如果我想要所有从-1到1的随机有理数,两个边界都包括在内,那么这条线是否有效:Math.random() * 2.00000000000000001 - 1; 我查了一下,
本文向大家介绍javascript产生随机数方法汇总,包括了javascript产生随机数方法汇总的使用技巧和注意事项,需要的朋友参考一下 1.Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) 2.Math.floor(num); 参数num为一个数值,函数结果为num的整数部分。 3.Math.round(num); 参数num为一个数值,函数结果为num四舍五入后的
我在用数学。函数获取从0到“max”数字范围内的整数。但据sonarqube称,这是“安全热点”的媒介问题。
本文向大家介绍Java获取随机数的3种方法,包括了Java获取随机数的3种方法的使用技巧和注意事项,需要的朋友参考一下 主要介绍了Java获取随机数的3种方法,主要利用random()函数来实现 方法1 (数据类型)(最小值+Math.random()*(最大值-最小值+1))例: 从1到10的int型随数 方法2 获得随机数 通过java.Math包的random方法得到1-10的int随机数
每次产生一个随机数。 用法 Your browser does not support the video tag. 案例:掷骰子 功能:设置随机数范围1-6,每按一下按钮,产生一个随机数 工作原理 当输入由no变为yes时,一个随机数将会被传送到输出。你可以通过配置改变随机数的范围 例如:一个随机变色的灯