CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
The basic mathematical operators are the same as the ones available in most programming languages: addition (+), subtraction (-), division (/), multiplication (*) and modulus (%, produces the remainder from integer division). Integer division truncates, rather than rounds, the result.
Java also uses a shorthand notation to perform an operation and an assignment at the same time. This is denoted by an operator followed by an equal sign, and is consistent with all the operators in the language (whenever it makes sense). For example, to add 4 to the variable x and assign the result to x, use: x += 4
This example shows the use of the mathematical operators:
//: MathOps.java
// Demonstrates the mathematical operators
import java.util.*;
public class MathOps
// shorthand to print a string and an int:
static void pInt(String s, int i)
// shorthand to print a string and a float:
static void pFlt(String s, float f)
public static void main(String[] args)
} ///:~
The first thing you will see are some shorthand methods for printing: the prt( ) method prints a String, the pInt( ) prints a String followed by an int and the pFlt( ) prints a String followed by a float. Of course, they all ultimately end up using System.out.println( ).
To generate numbers, the program first creates a Random object. Because no arguments are passed during creation, Java uses the current time as a seed for the random number generator. The program generates a number of different types of random numbers with the Random object simply by calling different methods: nextInt( ), nextLong( ), nextFloat( ) or nextDouble( ).
The modulus operator, when used with the result of the random number generator, limits the result to an upper bound of the operand minus one (99 in this case).
The unary minus (-) and unary plus (+) are the same operators as binary minus and plus. The compiler figures out which use is intended by the way you write the expression. For instance, the statement
x = -a;
has an obvious meaning. The compiler is able to figure out:
x = a * -b;
but the reader might get confused, so it is more clear to say:
x = a * (-b);
The unary minus produces the negative of the value. Unary plus provides symmetry with unary minus, although it doesn't do much.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 665
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved