Java 浮子式铸造isn';t工作正常,始终计算为0.0
代码: 我在谷歌上搜索了一下,还没有找到答案。好吧,我输入的是正确的,但它的计算结果一直是0.0。在计算过程中,我甚至尝试将Java 浮子式铸造isn';t工作正常,始终计算为0.0,java,android,casting,floating-point,double,Java,Android,Casting,Floating Point,Double,代码: 我在谷歌上搜索了一下,还没有找到答案。好吧,我输入的是正确的,但它的计算结果一直是0.0。在计算过程中,我甚至尝试将360、percentage和100类型转换为float,但仍然不起作用 EDIT1: 完整代码: double percentage = 0.7711627906976745; mSweepAngle = (float)(360*(percentage/(double)100)); 请注意,已成功地从另一个活动传递了百分比,当前正在传递0.771162790697674
360
、percentage
和100
类型转换为float,但仍然不起作用
EDIT1:
完整代码:
double percentage = 0.7711627906976745;
mSweepAngle = (float)(360*(percentage/(double)100));
请注意,已成功地从另一个活动传递了百分比,当前正在传递0.7711627906976745。您正在从构造函数调用
init
,而执行此计算的是init
。因此,无论您使用什么机制来设置百分比
,在计算完成之前都不会发生
我郑重建议学习使用调试器。单步执行代码会立即告诉您发生了什么。当我运行此程序时(后面是
System.out.println(mSweepAngle);
),它给了我2.776186
。您确定除了此代码之外没有其他问题吗?事实上,您不需要所有这些铸件。由于percentage
已经是double
,它的除法和乘法返回double
值(编译器将int
转换为double
)。您提到的问题是,当您对整数进行除法时(v.g.4/10
返回0
),是否可能有一个名为msweapangle
的字段,以及一个名为msweapangle
的局部变量来屏蔽它?或者你在一个物体上做这个计算,然后在另一个物体上检查值?像这样的?请给我们看你所有的代码,不仅仅是这两行-这两行都很好。显示a。请检查我的编辑,我添加了完整的代码。
package se.kiendys.petrus.da171a.uppg3.budgetapp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class PieChartView extends View {
private Paint piechartItem1Paint;
private Paint piechartItem2Paint;
private Paint piechartBorderPaint;
private Paint infoBorderPaint;
private Paint infoBackgroundPaint;
private Paint infoTextPaint;
private RectF oval;
private int mWidth;
private int mHeight;
private int mMarginLeft;
private int mMarginTop;
private float mStartAngle;
private float mSweepAngle;
private double percentage;
public PieChartView(Context context) {
super(context);
init();
}
public PieChartView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init() {
piechartItem1Paint = new Paint(); // the piechart items color
piechartItem2Paint = new Paint(); // the piechart items color
piechartBorderPaint = new Paint(); // the piechart border color
infoTextPaint = new Paint(); // the info text color
infoBackgroundPaint = new Paint(); // the info background color
infoBorderPaint = new Paint(); // the info border color
mWidth = 200; // circle width
mHeight = 200; // circle height
mMarginLeft = 10; // circle and info margin
mMarginTop = 10; // circle and info margin
mStartAngle = 0; // the starting angle of the arc, begins at the rightmost part of the circle (an angle of 0 degrees correspond to the geometric angle of 0 degrees, or 3 o'clock on a watch)
// and through incrementation moves clockwise, the units are degrees
oval = new RectF(mMarginLeft, mMarginTop, mWidth, mHeight); // sets the shape and size (boundaries) of the drawn circle
piechartItem1Paint.setAntiAlias(true);
piechartItem1Paint.setStyle(Style.FILL);
piechartItem1Paint.setStrokeWidth(0.5f);
piechartItem2Paint.setAntiAlias(true);
piechartItem2Paint.setStyle(Style.FILL);
piechartItem2Paint.setStrokeWidth(0.5f);
piechartItem1Paint.setColor(0xCCFEFEFE); // blue color
piechartItem2Paint.setColor(0xCC343434); // green color
// double temp = (360*((double)percentage/(double)100));
// Log.d(BudgetConstants.DEBUG_TAG, "temp: "+temp);
// mSweepAngle = (float)temp;
// Log.d(BudgetConstants.DEBUG_TAG, "init mSweepAngle: "+mSweepAngle);
// mSweepAngle = (float)(360*(percentage/(double)100));
// mSweepAngle = (float)(percentage/100.0*360);
Log.d(BudgetConstants.DEBUG_TAG, "init mSweepAngle: "+mSweepAngle);
}
public void setDistributionPercentage(double percentage) {
this.percentage = percentage;
Log.d(BudgetConstants.DEBUG_TAG, "percentage: "+percentage);
}
protected void onDraw(Canvas canvas) {
Log.d(BudgetConstants.DEBUG_TAG, "onDraw fired!");
canvas.drawArc(oval, mStartAngle, mSweepAngle, true, piechartItem1Paint);
Log.d(BudgetConstants.DEBUG_TAG, "startAngle1: "+mStartAngle+", sweepAngle1: "+mSweepAngle);
canvas.drawArc(oval, mSweepAngle, (360-mSweepAngle), true, piechartItem2Paint);
Log.d(BudgetConstants.DEBUG_TAG, "startAngle2: "+mSweepAngle+", sweepAngle2: "+(360-mSweepAngle));
}
}