|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
麻烦大佬看看下面的哪里错了
import java.util.Scanner;
public class pta{
public static void main(String[] args){
double x;
double y;
Scanner myscanner = new Scanner(System.in);
x = myscanner.nextDouble();
if( x < 5 ){
y = x + 5;
String s = String.format("%.3d",y);
System.out.println("y=" + s);
}
else if(x >= 5 && x < 10){
y=x * x + x * 0.3;
String s = String.format("%.3d",y);
System.out.println("y=" + s);
}
else (x >= 10){
y=x * 5 - 10;
String s = String.format("%.3d",y);
System.out.println("y=" + s);
}
}
}
问题出在第26行,else语句的格式不正确。在else语句后面不应该再跟一个条件,应该直接跟着代码块。正确的写法是将else (x >= 10)改为else。修改后的代码如下:
- import java.util.Scanner;
- public class pta {
- public static void main(String[] args) {
- double x;
- double y;
- Scanner myscanner = new Scanner(System.in);
- x = myscanner.nextDouble();
- if (x < 5) {
- y = x + 5;
- String s = String.format("%.3f", y);
- System.out.println("y=" + s);
- } else if (x >= 5 && x < 10) {
- y = x * x + x * 0.3;
- String s = String.format("%.3f", y);
- System.out.println("y=" + s);
- } else {
- y = x * 5 - 10;
- String s = String.format("%.3f", y);
- System.out.println("y=" + s);
- }
- }
- }
复制代码
此外,还有一个小问题是在使用String.format方法时,应该使用"%.3f"而不是"%.3d"来格式化浮点数。
|
|