首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试将浮点型转换为整型时出现问题

尝试将浮点型转换为整型时出现问题
EN

Stack Overflow用户
提问于 2016-03-15 10:48:05
回答 2查看 386关注 0票数 1

我有一个编程任务,我要把一个地震的大小和另一个地震的大小进行比较。我应该使用Comparable接口并实现compareTo方法。地震震级是float值,compareTo方法应该返回int类型。所以我的代码看起来像这样:

代码语言:javascript
复制
 public int compareTo(EarthquakeMarker marker){
    return ((int)this.getMagnitude().compareTo((int)marker.getMagnitude()));     
 }

但我得到了一个错误,说我“无法调用int compareTo()到基本类型浮点”。

对于感兴趣的人,这是完整的代码:

代码语言:javascript
复制
package module6;

import de.fhpotsdam.unfolding.data.PointFeature;
import processing.core.PConstants;
import processing.core.PGraphics;

/** Implements a visual marker for earthquakes on an earthquake map
 * 
 * @author UC San Diego Intermediate Software Development MOOC team
 *
 */
// TODO: Implement the comparable interface
public abstract class EarthquakeMarker extends CommonMarker implements Comparable<EarthquakeMarker>
{

// Did the earthquake occur on land?  This will be set by the subclasses.
protected boolean isOnLand;

// The radius of the Earthquake marker
// You will want to set this in the constructor, either
// using the thresholds below, or a continuous function
// based on magnitude. 
protected float radius;


// constants for distance
protected static final float kmPerMile = 1.6f;

/** Greater than or equal to this threshold is a moderate earthquake */
public static final float THRESHOLD_MODERATE = 5;
/** Greater than or equal to this threshold is a light earthquake */
public static final float THRESHOLD_LIGHT = 4;

/** Greater than or equal to this threshold is an intermediate depth */
public static final float THRESHOLD_INTERMEDIATE = 70;
/** Greater than or equal to this threshold is a deep depth */
public static final float THRESHOLD_DEEP = 300;

// ADD constants for colors


// abstract method implemented in derived classes
public abstract void drawEarthquake(PGraphics pg, float x, float y);


// constructor
public EarthquakeMarker (PointFeature feature) 
{
    super(feature.getLocation());
    // Add a radius property and then set the properties
    java.util.HashMap<String, Object> properties = feature.getProperties();
    float magnitude = Float.parseFloat(properties.get("magnitude").toString());
    properties.put("radius", 2*magnitude );
    setProperties(properties);
    this.radius = 1.75f*getMagnitude();
}

// TODO: Add the method:
 public int compareTo(EarthquakeMarker marker){
    return ((int)this.getMagnitude().compareTo((int)marker.getMagnitude());

 }


// calls abstract method drawEarthquake and then checks age and draws X if needed
@Override
public void drawMarker(PGraphics pg, float x, float y) {
    // save previous styling
    pg.pushStyle();

    // determine color of marker from depth
    colorDetermine(pg);

    // call abstract method implemented in child class to draw marker shape
    drawEarthquake(pg, x, y);

    // IMPLEMENT: add X over marker if within past day      
    String age = getStringProperty("age");
    if ("Past Hour".equals(age) || "Past Day".equals(age)) {

        pg.strokeWeight(2);
        int buffer = 2;
        pg.line(x-(radius+buffer), 
                y-(radius+buffer), 
                x+radius+buffer, 
                y+radius+buffer);
        pg.line(x-(radius+buffer), 
                y+(radius+buffer), 
                x+radius+buffer, 
                y-(radius+buffer));

    }

    // reset to previous styling
    pg.popStyle();

}

/** Show the title of the earthquake if this marker is selected */
public void showTitle(PGraphics pg, float x, float y)
{
    String title = getTitle();
    pg.pushStyle();

    pg.rectMode(PConstants.CORNER);

    pg.stroke(110);
    pg.fill(255,255,255);
    pg.rect(x, y + 15, pg.textWidth(title) +6, 18, 5);

    pg.textAlign(PConstants.LEFT, PConstants.TOP);
    pg.fill(0);
    pg.text(title, x + 3 , y +18);


    pg.popStyle();

}


/**
 * Return the "threat circle" radius, or distance up to 
 * which this earthquake can affect things, for this earthquake.   
 * DISCLAIMER: this formula is for illustration purposes
 *  only and is not intended to be used for safety-critical 
 *  or predictive applications.
 */
public double threatCircle() {  
    double miles = 20.0f * Math.pow(1.8, 2*getMagnitude()-5);
    double km = (miles * kmPerMile);
    return km;
}

// determine color of marker from depth
// We use: Deep = red, intermediate = blue, shallow = yellow
private void colorDetermine(PGraphics pg) {
    float depth = getDepth();

    if (depth < THRESHOLD_INTERMEDIATE) {
        pg.fill(255, 255, 0);
    }
    else if (depth < THRESHOLD_DEEP) {
        pg.fill(0, 0, 255);
    }
    else {
        pg.fill(255, 0, 0);
    }
}


/** toString
 * Returns an earthquake marker's string representation
 * @return the string representation of an earthquake marker.
 */
public String toString()
{
    return getTitle();
}
/*
 * getters for earthquake properties
 */

public float getMagnitude() {
    return Float.parseFloat(getProperty("magnitude").toString());
}

public float getDepth() {
    return Float.parseFloat(getProperty("depth").toString());   
}

public String getTitle() {
    return (String) getProperty("title");   

}

public float getRadius() {
    return Float.parseFloat(getProperty("radius").toString());
}

public boolean isOnLand()
{
    return isOnLand;
}

}

EN

回答 2

Stack Overflow用户

发布于 2016-03-15 10:50:50

有一个内置的静态便利函数Float.compare(float, float),用于比较基本的浮点数:

代码语言:javascript
复制
public int compareTo(EarthquakeMarker marker){
    return Float.compare(this.getMagnitude(), marker.getMagnitude());
}
票数 5
EN

Stack Overflow用户

发布于 2016-03-15 10:50:48

正如消息所说,您不能调用原语类型的方法。

你应该自己实现compareTo(),因为它很简单,而且我认为每次调用compareTo()时创建像Integer这样的对象的成本可能太高了。

代码语言:javascript
复制
public int compareTo(EarthquakeMarker marker){
    int t = (int)this.getMagnitude()
    int m = (int)marker.getMagnitude();
    if (t > m) return 1;
    if (t < m) return -1;
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36001783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档