根据用户输入打印回计算

时间:2016-02-02 03:59:16

标签: java math user-input

所以我试图获得类似于此的输出:

Enter the first number: 3.25

Enter the second number: 10.3

  3.25 *  10.30 =  33.48

  3.25 /  10.30 =   0.32

  3.25 %  10.30 =   3.25

 10.30 *   3.25 =  33.48

 10.30 /   3.25 =   3.17

 10.30 %   3.25 =   0.55

在此示例中,用户输入数字3.25和10.3,然后代码获取数字并对其应用多个计算。

所以这就是我到目前为止所做的:

import java.util.Scanner;

public class test {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

double fnumber, snumber, mult, div, modulo, mult2, div2, modulo2;
System.out.println("Enter the first number:");
fnumber = scan.nextDouble();

System.out.println("Enter the second number:");
snumber = scan.nextDouble();

/* various calculations  */
mult = fnumber * snumber;
div = fnumber / snumber;
modulo = fnumber % snumber;
mult2 = snumber * fnumber;
div2 = snumber / fnumber;
modulo2 = snumber % fnumber;

/* print back to user  */
System.out.printf("%f * %f = %.2f\n", mult);

直到最后一行我还好。从上面的例子中我试图说出3.25 * 10.30 = 33.48

如何使用%f根据用户输入调用多个双打?

感谢。

2 个答案:

答案 0 :(得分:0)

你几乎拥有它。试试这个:

System.out.printf("%f * %f = %f\n", fnumber, snumber, mult);

答案 1 :(得分:0)

尝试:

System.out.printf("%f * %f = %.2f\n", fnumber, snumber, mult);