多个微调器计算结果不准确

时间:2013-11-21 08:26:08

标签: android spinner

我是Android开发新手我有一个calulator页面,我需要对从两个微调器中选择的选项进行小计算。我使用了switch case来获得结果。

所有用户界面都按预期进行,但是当我使用按钮计算时,结果不符合预期和随机。

任何人都可以指出我犯的错误吗?

活性

public class Calculatorrecon extends Activity implements OnItemSelectedListener {

    double size, speed, result1, result2;
    Button but1;
    TextView result;
    Spinner shoesize, shoespeed;
    String[] size = { "Select Size", "100 cm", "80 cm",
            "70 cm", "60 cm", "50 cm", "40 cm", "30 cm", };
    String[] speed = { "Select Speed", "40", "50", "60", "70", "80", "90", "100", };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator);
        result = (TextView) findViewById(R.id.speed);
        but1 = (Button) findViewById(R.id.but);
        ArrayAdapter<String> speedadapter = new ArrayAdapter<String>(
                Calculatorrecon.this, android.R.layout.simple_spinner_item,
                speed);
        speedadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        shoespeed = (Spinner) findViewById(R.id.speed48);
        shoespeed.setAdapter(speedadapter);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                Calculatorrecon.this, android.R.layout.simple_spinner_item,
                size);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        shoesize = (Spinner) findViewById(R.id.size);
        shoesize.setAdapter(adapter);

        shoesize.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        int positition = shoesize.getSelectedItemPosition();
        switch (positition) {
        case 0:

            break;
        case 1:
            size = 100;
            break;
        case 2:
            size = 80;
            break;
        case 3:
            size = 70;
            break;
        case 4:
            size = 60;
            break;
        case 5:
            size = 50;
            break;
        case 6:
            size = 40;
            break;
        case 7:
            size = 30;
            break;

        }
        int speedz = shoespeed.getSelectedItemPosition();
        switch (speedz) {
        case 0:

            break;
        case 1:
            speed = 4;
            break;
        case 2:
            speed = 5;
            break;
        case 3:
            speed = 6;
            break;
        case 4:
            speed = 7;
            break;
        case 5:
            speed = 8;
            break;
        case 6:
            speed = 9;
            break;
        case 7:
            speed = 10;
            break;

        }
        but1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                calculate();

                result.setText(String.format("%.1f", result2) + "");
            }
        });

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

    private void calculate() {

        result2 = 0;

        double x = size * 8.4563764;
        double y = 167.2 * speed;
        result2 = y / x;

    }

1 个答案:

答案 0 :(得分:1)

试试这个..

1)您的doubleString[]变量名称 相同

Spinner shoesize;
    Spinner shoespeed;
    Button btn;

    String[] size = { "Select Size", "100 cm", "80 cm", "70 cm", "60 cm",
            "50 cm", "40 cm", "30 cm", };
    String[] speed = { "Select Speed", "40", "50", "60", "70", "80", "90",
            "100", };

    double final_size, final_speed, result2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator);

        btn = (Button) findViewById(R.id.btn);
        shoesize = (Spinner) findViewById(R.id.spinner1);
        shoespeed = (Spinner) findViewById(R.id.spinner2);

        ArrayAdapter<String> speedadapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, speed);
        speedadapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        shoesize.setAdapter(speedadapter);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, size);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        shoespeed.setAdapter(adapter);

        shoesize.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View v, int pos,
                    long id) {
                // TODO Auto-generated method stub
                int speedz = shoesize.getSelectedItemPosition();
                switch (speedz) {
                case 0:

                    break;
                case 1:
                    final_speed = 4;
                    break;
                case 2:
                    final_speed = 5;
                    break;
                case 3:
                    final_speed = 6;
                    break;
                case 4:
                    final_speed = 7;
                    break;
                case 5:
                    final_speed = 8;
                    break;
                case 6:
                    final_speed = 9;
                    break;
                case 7:
                    final_speed = 10;
                    break;

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });

        shoespeed.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View v, int pos,
                    long id) {
                // TODO Auto-generated method stub
                int positition = shoespeed.getSelectedItemPosition();
                switch (positition) {
                case 0:

                    break;
                case 1:
                    final_size = 100;
                    break;
                case 2:
                    final_size = 80;
                    break;
                case 3:
                    final_size = 70;
                    break;
                case 4:
                    final_size = 60;
                    break;
                case 5:
                    final_size = 50;
                    break;
                case 6:
                    final_size = 40;
                    break;
                case 7:
                    final_size = 30;
                    break;

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                result2 = calculate();
                 Log.v("result", ""+String.format("%.1f", result2));
                            result.setText(String.format("%.1f", result2) + "");
            }

        });

    }

    protected double calculate() {

            double x = final_size * 8.4563764;
            double y = 167.2 * final_speed;
            double result2 = y / x;

        return result2;

    }