English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce programme, vous apprendrez à multiplier deux nombres flottants en Java, à stocker le résultat et à l'afficher à l'écran.
public class MultiplyTwoNumbers { public static void main(String[] args) { float first = 1.5f; float second = 2.0f; float product = first * second; System.out.println("Calculation result: ", + product); } }
When running the program, the output is:
Calculation result: 3.0
In the above program, we have two floating-point numbers1.5f and2.0f stored in variables first and second separately.
Note that we have used f after the number. This ensures that the number is a float, otherwise it will be assigned as a double type.
Then use multiplication*operator, and store the result of multiplying first and second in a new float variable product.
Finally, use the println() function to print the product result on the screen.