对于小数和小数点位数都为变量,用java怎么写
发布网友
发布时间:2022-05-31 19:20
我来回答
共1个回答
热心网友
时间:2023-10-24 12:42
import java.util.Scanner;
public class Demo1 {
\x09/**
\x09 * @param args
\x09 */
\x09public static void main(String[] args) {
\x09\x09// TODO Auto-generated method stub
\x09\x09int a=0,b=0,c=0;
\x09\x09double d = 0;
\x09\x09Scanner scanner = new Scanner (System.in);
\x09\x09System.out.print("请输入被除数:");
\x09\x09a = scanner.nextInt();
\x09\x09System.out.print("请输入除数:");
\x09\x09b = scanner.nextInt();
\x09\x09System.out.print("请输入保留小数的位数:");
\x09\x09c = scanner.nextInt();
\x09\x09d = (double)a/b;
\x09\x09String s = "%." + c + "f\n";
\x09\x09System.out.printf("结果为: " + a + "/" + b + " = " + s, d);
\x09}
}