索千 2023-04-08 20:09 采纳率: 74.4%
浏览 9
已结题

代码运行问题,关于类实例化运用

// 为什么执行完下面一步,变量 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();
    }

}

  • 写回答

2条回答 默认 最新

  • Mini厂程序员 2023-04-08 20:18
    关注

    你在设置完height之后,h又被指向了一个新的Cone对象,这个新的对象height默认为初始化为0,为什么h对象要频繁地创建?你这代码看着真的头疼啊

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月14日
  • 修改了问题 4月8日
  • 创建了问题 4月8日

悬赏问题

  • ¥15 船舶旋回实验matlab
  • ¥30 SQL 数组,游标,递归覆盖原值
  • ¥15 为什么我的数据接收的那么慢呀有没有完整的 hal 库并 代码呀有的话能不能发我一份并且我用 printf 函数显示处理之后的数据,用 debug 就不能运行了呢
  • ¥15 有关于推荐系统jupyter
  • ¥20 gitlab 中文路径,无法下载
  • ¥15 用动态规划算法均分纸牌
  • ¥30 udp socket,bind 0.0.0.0 ,如何自动选取用户访问的服务器IP来回复数据
  • ¥15 关于树的路径求解问题
  • ¥15 yolo在训练时候出现File "D:\yolo\yolov5-7.0\train.py"line 638,in <module>
  • ¥30 戴尔inspiron独显直连