int totalOptCount = 500;
int totalRespCount=1500;
float percentage =(float)(totalOptCount/totalRespCount);
Run Code Online (Sandbox Code Playgroud)
为什么这总是返回值0.0?另外我想将其格式化为00.00格式并转换为字符串?
我在转换这个公式时遇到了问题V = 4/3 ? r^3.我用Math.PI和Math.pow,但是这是问题的开始位置.我得到这个错误(每次),
';' 预期
此外,直径变量不起作用.那里有错误吗?
import java.util.Scanner;
import javax.swing.JOptionPane;
public class NumericTypes
{
public static void main (String [] args)
{
double radius;
double volume;
double diameter;
diameter = JOptionPane.showInputDialog("enter the diameter of a sphere.");
radius = diameter / 2;
volume = (4 / 3) Math.PI * Math.pow(radius, 3);
JOptionPane.showMessageDialog("The radius for the sphere is "+ radius
+ "and the volume of the sphere is ");
}
}
Run Code Online (Sandbox Code Playgroud) 这是一个例子:
Double d = (1/3);
System.out.println(d);
Run Code Online (Sandbox Code Playgroud)
这将返回0,而不是0.33333 ......应该如此.
有人知道吗?
在我的任务中,我必须制作一个简单版本的Craps,由于某种原因,即使两个变量都是非0,百分比赋值总是产生0,这里是代码.
import java.util.Random;
Run Code Online (Sandbox Code Playgroud)
标题,注意变量
public class Craps {
private int die1, die2,myRoll ,myBet,point,myWins,myLosses;
private double winPercent,lossPercent;
private Random r = new Random();
Run Code Online (Sandbox Code Playgroud)
只需滚动两个模具并生产一些模具.
public int roll(){
die1 = r.nextInt(6)+1;
die2 = r.nextInt(6)+1;
return(die1 + die2);
}
Run Code Online (Sandbox Code Playgroud)
Play方法,这只是循环游戏.
public void play(){
myRoll = roll();
point = 0;
if(myRoll == 2 ||myRoll == 3 || myRoll == 12){
System.out.println("You lose!");
myLosses++;
}else if(myRoll == 7 || myRoll == 11){
System.out.println("You win!");
myWins++;
}else{
point = myRoll;
do {
myRoll = roll();
}while(myRoll …Run Code Online (Sandbox Code Playgroud) public class test {
public static void main(String[] args) {
int total = 2;
int rn = 1;
double rnp = (rn / total) * 100;
System.out.println(rnp);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么它打印0.0而不是50.0?
https://www.google.com/search?q=100*(1%2F2)&aq=f&oq=100*(1%2F2)
我有以下java代码
long x=25782
float y=x/1000000000
Run Code Online (Sandbox Code Playgroud)
而不是像我期望的那样y等于0.000025782,它等于0.0
我究竟做错了什么?
我正在制作一个简单的计算器,但是当我尝试划分时,我总是得到1.0,当我减去时,我总是得到零.加法和乘法工作正常.
我读过类似的问题,例如: 为什么整数除法代码给出了错误的答案? 在Java中划分两个整数给我0或100? Java中的除法总是导致零(0)?
还有很多...
这是我的代码的一部分:
if (minu) {
number1 = Double.valueOf(display1.getText().toString());
number2 = Double.valueOf(display1.getText().toString());
display1.setText("");
display2.setText("");
displaySymbol.setText("");
answer = number1 - number2;
display1.setText(Double.toString(answer));
}
Run Code Online (Sandbox Code Playgroud)
和分裂:
if (divid) {
number1 = Double.valueOf(display1.getText().toString());
number2 = Double.valueOf(display1.getText().toString());
display1.setText("");
display2.setText("");
displaySymbol.setText("");
answer = number2 / number1;
display1.setText(Double.toString(answer));
}
Run Code Online (Sandbox Code Playgroud)
请帮忙!