Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android SeekBar值未正确传递到计算方法_Java_Android - Fatal编程技术网

Java Android SeekBar值未正确传递到计算方法

Java Android SeekBar值未正确传递到计算方法,java,android,Java,Android,在OnSeekbarchangListener类的OnProgressChanged方法中,我有一个计算小费百分比并将其显示在屏幕上的方法。它似乎只在值0和100之间工作,而不在两者之间工作 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub progre

在OnSeekbarchangListener类的OnProgressChanged方法中,我有一个计算小费百分比并将其显示在屏幕上的方法。它似乎只在值0和100之间工作,而不在两者之间工作

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // TODO Auto-generated method stub
                progress = ((int)Math.round(progress/5 ))*5;
                seekBar.setProgress(progress);
                String tipValStr = String.valueOf(progress);
                tipVal = progress;
                tvTipVal.setText(tipValStr);
                //Toast.makeText(TipCalc.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
                calculate(progress);
            }

public void calculate(int tip){
        try{
        DecimalFormat df = new DecimalFormat("#.00");
        billVal = Double.parseDouble(etBillVal.getText().toString());
        //tipVal = sbTipVal.getProgress();
        tipValue = billVal * (tip/100);
        billTotal = billVal + tipValue;
        tvBillVal.setText("$"+df.format(billVal));
        tvTipValue.setText("+ $" + df.format(tipValue));
        tvBillTotal.setText("$"+df.format(billTotal));
        Toast.makeText(TipCalc.this, "$"+df.format(billTotal), Toast.LENGTH_SHORT).show();
        }
        catch(Exception e){
            //billVal = 0;
        }
    }

我猜你们的问题是整数除法。int
tip
除以
100
,将始终返回0,除非
tip
值为100,在这种情况下,结果为
1

尝试将
tip
定义为
float
-

public void calculate(float tip);

很高兴有帮助:请考虑投票/接受答案,以便其他用户可以发现它是正确的。
tipValue = billVal * (tip / 100.0f);