-4

我无法真正弄清楚下面代码中的错误是什么。另外:我的代码看起来很不雅,有什么建议可以更好地构建它吗?

import java.util.*;
import java.io.*;
import java.lang.Math;

public class FlightSimulator {
public static void main(String[] args){

    int time;
    int v_y;
    int v;
    int v_squart;
    int height;


    Scanner myscan = new Scanner(System.in);
    System.out.print("Please enter the time for which you want to output the height of the " +
            "plane ");
    time = myscan.nextInt();
    if(time==0){
    System.out.print("The height of the plane is 456 meters over ground."); 

    }
    else{
        v_y = 51 - time*3;
        v = Math.pow(v_y+20, 2);
        v_squart = Math.sqrt(v);
        height = 456 - v;
        System.out.print("The height of the plane is" + height);

    }
}
}
4

1 回答 1

1
v_y = 51 - time*3;
v = (int)Math.pow(v_y+20, 2);
v_squart = (int)Math.sqrt(v); // why take the square root of something you just squared...
height = 456 - v;
System.out.print("The height of the plane is" + height);

整数不能包含十进制值,Math.pow 和 Math.sqrt 都是返回double类型。您已将和声明为类型v_y,您必须将操作转换为整数。您还可以将变量声明为类型vv_squartintdouble

int time;
double v_y;
double v;
double v_squart;
double height;
于 2013-10-05T21:59:27.060 回答