原文: https://www.programiz.com/java-programming/examples/multiply-floating-numbers
public class MultiplyTwoNumbers {
public static void main(String[] args) {
float first = 1.5f;
float second = 2.0f;
float product = first * second;
System.out.println("The product is: " + product);
}
}运行该程序时,输出为:
The product is: 3.0在上面的程序中,我们有两个浮点数1.5f和2.0f分别存储在变量first和second中。
注意,我们在数字后面使用了f。 这样可以确保编号为float,否则将为它们分配类型double。
然后使用*运算符将first和second相乘,并将结果存储在新的浮点变量product中。
最后,使用println()函数将结果product打印在屏幕上。