求解 在java中 用printf输出包流量为有效数字的书,怎么输出?

这是我的代码
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
double h = sc.nextInt();
double n = sc.nextInt();
shuai (h, n);
}
public static void shuai(double x, double y){
int i;
double q, w;
for (i = 0 ; i < y ; i++){
q = x + x / Math.pow(2, i);
w = y / 2.0;
}
System.out.printf("%.2lf %.2lf\n", q, w);
}
}
问题是 在java中 用printf输出保留两位有效数字的书,怎么输出?
我的代码哪里错了?

首先你没初始化,这个应该会报错,然后double类型是%f
和Cpp不一样
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
double h = sc.nextInt();
double n = sc.nextInt();
shuai (h, n);
}
public static void shuai(double x, double y){
int i;
double q=0, w=0;
for (i = 0 ; i < y ; i++){
q = x + x / Math.pow(2, i);
w = y / 2.0;
}
System.out.printf("%.2f %.2f\n", q, w);
}
}
温馨提示:答案为网友推荐,仅供参考