-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);

整数に 10 進値を含めることはできません。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 に答える