如何在Java中除法两个整数并返回一个double

如何在Java中除法两个整数并返回一个double,java,android,Java,Android,我是Android新手,我正在创建一个数学应用程序,但遇到了麻烦 基本前提是向用户提供2个介于0和20之间的数字(questionTextView),然后向用户显示一个包含3个错误答案和1个正确答案的网格。然后用户单击正确答案 我遇到的问题是,正确的答案没有显示到小数点后2位 例如问题:4/7 答复1:12.59 答复2:15.99 答复3:9.93 答案4:0(应为0.57) 我不明白为什么正确答案没有正确显示,因为我已经将整数转换为双精度整数,并包含十进制格式 我尝试了Math.round(

我是Android新手,我正在创建一个数学应用程序,但遇到了麻烦

基本前提是向用户提供2个介于0和20之间的数字(questionTextView),然后向用户显示一个包含3个错误答案和1个正确答案的网格。然后用户单击正确答案

我遇到的问题是,正确的答案没有显示到小数点后2位

例如问题:4/7

答复1:12.59

答复2:15.99

答复3:9.93

答案4:0(应为0.57)

我不明白为什么正确答案没有正确显示,因为我已经将整数转换为双精度整数,并包含十进制格式

我尝试了Math.round(),但我无法实现这一点-可能是因为我在for循环中生成问题的方式

错误答案正确显示

如蒙协助,将不胜感激

这是我的密码:

private static DecimalFormat df2 = new DecimalFormat("#.##");

public void generateQuestion(){

    //Create 2 random numbers between 0 and 20.
    Random rand = new Random();

    int a = rand.nextInt(21);
    int b = rand.nextInt(21);

    if (a==b){
        b = rand.nextInt(21);
    }

    questionTextView.setText(Integer.toString(a) + " / " + Integer.toString(b));

/*Create a random number between 0 and 3 to determine the grid square of 
the correct answer */
    locationOfCorrectAnswer = rand.nextInt(4);

    //Calculate the correct answer.
    double correctAnswer = (int)(((double)a/(double)b));

  //Generate an incorrect answer in case the correct answer is 
   randomly generated.
   double inCorrectAnswer;

    /*Loop through each square and assign either the correct answer or
    a randomly generated number. */
    for (int i=0; i<4; i++){
        if (i == locationOfCorrectAnswer){ 
            answers.add(df2.format(correctAnswer).toString());
        } else {
            inCorrectAnswer = 0.05 + rand.nextDouble() *20.0;

            while (inCorrectAnswer == correctAnswer){
                inCorrectAnswer = 0.05 + rand.nextDouble() *20.0;
            }
            answers.add(df2.format(inCorrectAnswer).toString());
        }
    }

    //Assign an answer to each of the buttons.
    button0.setText((answers.get(0)));
    button1.setText((answers.get(1)));
    button2.setText((answers.get(2)));
    button3.setText((answers.get(3)));
private static DecimalFormat df2=新的DecimalFormat(“#.##”);
公共无效生成请求(){
//创建2个介于0和20之间的随机数。
Random rand=新的Random();
int a=兰特·耐克斯汀(21);
int b=兰特·耐克斯汀(21);
如果(a==b){
b=兰特耐克斯汀(21);
}
questionTextView.setText(Integer.toString(a)+“/”+Integer.toString(b));
/*创建一个介于0和3之间的随机数,以确定网格的平方
正确答案*/
正确答案的位置=兰特·奈克斯汀(4);
//计算正确答案。
double correctAnswer=(int)((double)a/(double)b));
//如果正确答案不正确,则生成错误答案
随机生成。
双重不正确回答;
/*在每个方块上循环并指定正确答案或答案
随机产生的数字*/

对于(int i=0;i您需要在除法之前将强制转换移除为int:

这一行:

double correctAnswer = (int)(((double)a/(double)b));
应该是这样的:

double correctAnswer = (((double)a/(double)b));
(((double)a/(double)b))
这将给你=0.57,然后这个
(int)
这将在这里将0.57转换为0
(int)(((double)a/(double)b));
因为整数只能容纳
整数
,因此十进制值将被截断

使用此选项可以保留十进制值

double correctAnswer = (((double)a/(double)b));
更新:要获得十进制结果,只需将一个操作数转换为double,编译器将对第二个参数进行优化

更新信用证:@stackoverflowuser2010和@cricket_007

double correctAnswer = (double)a/b; 

这也可以在不使用
显式
转换的情况下通过类型转换或由编译器执行的类型转换完成

示例学分:@Andreas

double correctAnswer = a;  // a , will automatically converted to double 
correctAnswer /= b;
选择以下选项之一:

double correctAnswer = (double)a/b;


您只需要转换一个值,这两个值都不是必需的@cricket_007; agreed@cricket感谢大家的帮助。我确信我需要指定我正在转换的值的原始类型(int),因此是(int)(((double)a/(double)b));将分子或分母类型转换为双精度将返回双精度值,或者不转换:
double correctAnswer=a;correctAnswer/=b;
double correctAnswer = a/(double)b;
double correctAnswer = (double)a/(double)b;