City Pedia Web Search

Search results

  1. Results From The WOW.Com Content Network
  2. Getting random numbers in Java - Stack Overflow

    stackoverflow.com/questions/5887709

    The first solution is to use the java.util.Random class: import java.util.Random; Random rand = new Random(); // Obtain a number between [0 - 49]. int n = rand.nextInt(50); // Add 1 to the result to get a number from the required range. // (i.e., [1 - 50]). n += 1; Another solution is using Math.random():

  3. The Math.Random class in Java is 0-based. So, if you write something like this: Random rand = new Random(); int x = rand.nextInt(10); x will be between 0-9 inclusive. So, given the following array of 25 items, the code to generate a random number between 0 (the base of the array) and array.length would be:

  4. math - Calculate Average Java - Stack Overflow

    stackoverflow.com/questions/26852525

    You would use the Scanner class to get input from the user. For instance, if you wanted the user to enter in some names or numbers. In this case, it doesn't look like you need to request any input from the user so this line can probably be deleted. And lastly, the problem with displaying your output is in this line:

  5. Why can't my program access the math methods in Java?

    stackoverflow.com/questions/16693482

    15. Delete the file Math.java that you have probably inadvertently left in your source code directory. This file is preventing the normal usage of JDK's java.lang.Math class. You should also note that defining classes in the default package is bad practice and will cause various issues for you along the way.

  6. java - Use Math class to calculate - Stack Overflow

    stackoverflow.com/questions/2481443

    2*Math.PIMath.POW You forgot to put an asterisk between PI and Math, so the compiler sees PIMath as a symbol. It might be good to put parentheses to see this: 2*(Math.PI)*(Math.pow(radius, 2)) Note that Math.PI is a statically referenced constant. Math.pow, on the other hand, is a method. All identifiers in Java are case sensitive.

  7. Oct 19, 2014 at 0:45. 1. Yes, Math is a standard Java class. Scanner is, too. You have to import it because it lives in the java.util package. – Code-Apprentice. Oct 19, 2014 at 0:45. 1. Every class documented here is considered standard.

  8. If you want the maximum of three, use Math.max(MY_INT1, Math.max(MY_INT2, MY_DOUBLE2)). There must be a better way when there are n values involved. @mlissner Yes, use a loop and a variable max, check for every variable whether or not they are larger than max, if so: set max to that variable.

  9. What's the difference between java.lang.Math and...

    stackoverflow.com/questions/4232231

    The Javadoc for the Math class provides some information on the differences between the two classes: Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict ...

  10. Just use Math.abs(x - y) or in your case java.lang.Math.abs(x - y) to specify that you want to use java.lang.Math, not your class. In case you just want to use just few methods from java.lang.Math you can just import them like. import static java.lang.Math.abs; and use it like. abs(10 - 20) You can also try this way.

  11. Because factorial grows so quickly, stack overflow is not an issue if you use recursion. In fact, the value of 20! is the largest one can represent in a Java long. So the following method will either calculate factorial (n) or throw an IllegalArgumentException if n is too big. public long factorial(int n) {.