// 为什么执行完下面一步,变量 height 被置0了? 导致后面结果都是0
该如何改?希望上代码
package first;
import java.util.Scanner;
/*
* 输入法有若干个案例,每个案例一行,k、h、t1、t2… (其中,k表示锥体底面类型,h表示高)。
当k=1时,表示圆,随后的参数是半径。
当k=2时,表示正方形,随后的参数是边长。
当k=3时,表示三角形,随后的参数是两边一夹角。
Sample Input:
1 3 1
2 6 2
3 3 2 2 60
Sample Output:
3.14
8.00
1.73
*/
class Cone<E>{
double height ;
E bottom ;
public Cone(){
}
public Cone (E b){
bottom = b ;
}
public void setHeight(double h){
height = h ;
}
public double computerVolume(){
String s = bottom.toString() ;
double area = Double.parseDouble(s);
return 1.0/3.0*area*height ;
}
}
public class oj1539 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
double pi = 3.1415926 ;
while(sc.hasNext()){
int k = sc.nextInt() ;
Cone h = new Cone() ;
if(k == 1){
h.setHeight(sc.nextDouble());
int r = sc.nextInt() ;
double sy = pi*r*r ;
h = new Cone<Double>( sy );
// 为什么执行完下面一步,变量 height 被置0了? 导致后面结果都是0
double p1 = h.computerVolume();
System.out.println(String.format("%.2f", p1));
}
if(k == 2){
h.setHeight(sc.nextDouble());
int a = sc.nextInt() ;
double sz = a*a ;
h = new Cone<Double>(sz);
// 为什么执行完下面一步,变量 height 被置0了? 导致后面结果都是0
double p2 = h.computerVolume() ;
System.out.println(String.format("%.2f", p2));
}
if(k == 3){
h.setHeight(sc.nextDouble());
int a = sc.nextInt() ;
int b = sc.nextInt() ;
double c = sc.nextDouble() ;
double d = c/180*pi ;
double sj = a*b*Math.sin(d ) ;
h = new Cone<Double>(sj);
// 为什么执行完下面一步,变量 height 被置0了? 导致后面结果都是0
double p3 = h.computerVolume() ;
System.out.println(String.format("%.2f", p3));
}
}
sc.close();
}
}