Use Math.pow(double, double), or statically import pow as such:
import static java.lang.Math.pow;
Answer from Stefan Kendall on Stack OverflowVideos
Use Math.pow(double, double), or statically import pow as such:
import static java.lang.Math.pow;
Sure, you just need to call Math.pow(...), as it's a static method in the Math class:
for (int i = 0; i < n; i++) {
functionG[i] = Math.pow(1.25, i);
}
Note that I've changed it to use i rather than n as the second argument.
You could also get your original code to compile using:
import static java.lang.Math.pow;
in the import statements at the top of your code. See the Java Language Specification, section 7.5.3 for details of how this works.
Looks like codename1 overrides some of the java math functions.They can be accessed with codename1's MathUtil class. Thank you all for your time.
Those functions are available from the java.lang.Math class. You could statically import them all - import static java.lang.Math.*;.
I'm writing a program to calculate the wind chill based on a temperature and wind speed entered by the user. The formula to calculate wind chill is
Wind Chill = 35.74 + 0.6215T - 35.75V0.16 + 0.4275TV0.16
My current code is
import java.util.Scanner;
import java.util.Math;
public class WindChill {
public static void main(String[] args) {
//Declare variables
double windChill;
double temperature;
int windSpeed;
//Ask the user for temperature and wind speed
System.out.print("Enter the temperature (Fahrenheit): ");
Scanner input = new Scanner (System.in);
temperature = input.nextDouble();
System.out.print("Enter the wind speed(mph): ");
windSpeed = input.nextInt();
//Display the wind chill value
windChill = (35.74+(0.6215*temperature)) - (Math.pow(35.75*windSpeed,0.16)) + (Math.pow(0.4275*temperature*windSpeed,0.16));
System.out.print("The wind chill index is " + windChill);
}}
I get an error in the IDE on line 2 that says "the import java.util.Math could not be resolved". Also, when I try to run the program, I get the error message
"Exception in thread "main" java.lang.Error: Unresolved compilation problem: at WindChill.main(WindChill.java:6)
I checked my installed JREs and I am using JRE7. Can anybody tell me what is wrong?
import java.util.Scanner; public class FormulaEvaluator { /* *Evaluate the expression. * *@param args Command line arguments (not used). */
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String name = "";
double x = 0.0;
double value = 0.0;
//Prompt the user for a value of x
System.out.print("Enter a value for x: ");
x = userInput.nextDouble();
//Calculate the value of x using user input
value = Math.pow(x);
System.out.print("Result:" + value);} }
My error message: FormulaEvaluator.java:27: error: method pow in class Math cannot be applied to given types; value = Math.pow(x); ^ required: double,double found: double reason: actual and formal argument lists differ in length 1 error
We're having to solve a long formula as a project but when I try to raise the x variable, which is the user input, to a power it doesn't compile and gives the error. It works fine when using Math.abs so I'm not really sure what's going on. Thanks in advance.