计算错误?或者是其他东西?

时间:2011-08-18 18:06:36

标签: java android math

所以我不确定我是否只是对问题视而不见或者发生了什么事。从我检查的一切,这应该工作得很好。我猜这里有一些小东西,我没有注意到,我希望别人会抓住它,因为我没有看到它。基本上,第一个类有旋转器,它需要输入并将它们全部保存,加载旧信息并将新信息添加到它们中。然后它进行计算,给我一个新的双重。第二类是显示它们的内容。由于某种原因,它始终将onbasePerc显示为0.000。它应该做的计算应该给我一个数字,我不知道为什么不是。

这是输入类

public class AddGameBR extends Activity{

private static final String[] stealAttempt = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20"};
private static final String[] stolenBase = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20"};
private static final String[] caughtSteal = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20"};
private static final String[] runs = {"0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15","16","17","18","19","20"};

double stolenPerc=0;
int sa=0, sb=0, caught=0, run=0;

double ostolenPerc=0;
int osa=0, osb=0, ocaught=0, orun=0;

double nstolenPerc=0;
int nsa=0, nsb=0, ncaught=0, nrun=0;

public static final String PREFS_NAME = "GameSaved";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addgamebr);

    oldStats();

    //Baserunning Stats

    final Spinner spin16 = (Spinner) findViewById(R.id.spin16);
    ArrayAdapter<String> spinn16 = new
        ArrayAdapter<String>(this, R.layout.spinner_entry, stealAttempt);
    spinn16.setDropDownViewResource(R.layout.spinner_entry);
    spin16.setAdapter(spinn16);

    final Spinner spin17 = (Spinner) findViewById(R.id.spin17);
    ArrayAdapter<String> spinn17 = new
        ArrayAdapter<String>(this, R.layout.spinner_entry, stolenBase);
    spinn17.setDropDownViewResource(R.layout.spinner_entry);
    spin17.setAdapter(spinn17);

    final Spinner spin18 = (Spinner) findViewById(R.id.spin18);
    ArrayAdapter<String> spinn18 = new
        ArrayAdapter<String>(this, R.layout.spinner_entry, caughtSteal);
    spinn18.setDropDownViewResource(R.layout.spinner_entry);
    spin18.setAdapter(spinn18);

    final Spinner spin19 = (Spinner) findViewById(R.id.spin19);
    ArrayAdapter<String> spinn19 = new
        ArrayAdapter<String>(this, R.layout.spinner_entry, runs);
    spinn19.setDropDownViewResource(R.layout.spinner_entry);
    spin19.setAdapter(spinn19);

    Button save = (Button) findViewById(R.id.save);
    save.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View calculate)
        {
             //Running


            sa = Integer.parseInt((String) spin16.getSelectedItem());
            sb = Integer.parseInt((String) spin17.getSelectedItem());
            caught = Integer.parseInt((String) spin18.getSelectedItem());
            run = Integer.parseInt((String) spin19.getSelectedItem());

            nsa = sa + osa;
            nsb = sb + osb;
            ncaught = caught + ocaught;
            nrun = run + orun;

            if(nsa==0)
                nstolenPerc=0;
            else
                nstolenPerc = nsb / nsa;

            if(sa<sb){
                AlertDialog.Builder builder = new AlertDialog.Builder(AddGameBR.this);
                builder.setMessage("You have more Stolen Bases than Steal Attempts. Please fix and then resubmit.")
                       .setCancelable(false)
                       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
                builder.show();
            }
            else{

                AlertDialog.Builder builder = new AlertDialog.Builder(AddGameBR.this);
                builder.setMessage("Are you sure these stats are correct? \n\nAfter submitting they are final.")
                       .setCancelable(false)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {


                               SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
                                SharedPreferences.Editor editor = settings.edit();
                                saveGame(editor);
                                editor.commit();

                               Intent i = new Intent(AddGameBR.this, ViewBR.class);
                                startActivity(i);
                                finish();
                           }
                       })
                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
                builder.show();
            }
        }
    });

}

public void saveGame(SharedPreferences.Editor map){
    if (map == null) {          
        return;
    }
    //Baserunning
            map.putInt("stealattempts", nsa);
            map.putInt("stolenbases", nsb);
            map.putInt("caughtstealing", ncaught);
            map.putInt("run", nrun);
            map.putString("stealPercentage", Double.toString(nstolenPerc));
}

public void oldStats(){
     SharedPreferences saved = this.getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
    //Baserunning
        osa = saved.getInt("stealattempts", 0);
        osb = saved.getInt("stolenbases", 0);
        ocaught = saved.getInt("caughtstealing", 0);
        orun = saved.getInt("run", run);
        ostolenPerc = Double.parseDouble(saved.getString("stealPercentage", "0"));
}

}

这是观看课程

public class ViewBR extends Activity{
public static final String PREFS_NAME = "GameSaved";

double stolenPerc=0;
int sa=0, sb=0, caught=0, run=0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewbr);

// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    oldStats();

    final DecimalFormat formatter = new DecimalFormat("0.000");

    if(sa==0)
        stolenPerc=0;
    else
        stolenPerc = sb / sa;

  //Base Running

    TextView stealattempts = (TextView)findViewById(R.id.stealattempts);
    stealattempts.setText("Steal Attempts: " + sa);

    TextView stolenbases = (TextView)findViewById(R.id.stolenbases);
    stolenbases.setText("Stolen Bases: " + sb);

    TextView caughtstealing = (TextView)findViewById(R.id.caughtstealing);
    caughtstealing.setText("Caught Stealing: " + caught);

    TextView runs = (TextView)findViewById(R.id.runs);
    runs.setText("Runs: " + run);

    TextView stolenpercentage = (TextView)findViewById(R.id.stolenpercentage);
    stolenpercentage.setText("Stolen Base Percentage: " + formatter.format(stolenPerc) + "%");

    Button copy = (Button) findViewById(R.id.copy_btn);
    copy.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View copy)
        {
            ClipboardManager clipboard = 
                      (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 

                 clipboard.setText("Base Running Stats:" +
                        "\nSteal Attempts: " + sa+
                        "\nStolen Bases: " + sb +
                        "\nCaught Stealing: " + caught +
                        "\nRuns: " + run +
                        "\nStolen Base Percentage: " + formatter.format(stolenPerc) + "%");
                 Toast.makeText(ViewBR.this, "Copied To Clipboard", 1).show();
        }
    });


}

public void oldStats(){
     SharedPreferences saved = this.getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);

    //Baserunning
        sa = saved.getInt("stealattempts", 0);
        sb = saved.getInt("stolenbases", 0);
        caught = saved.getInt("caughtstealing", 0);
        run = saved.getInt("run", run);
        stolenPerc = Double.parseDouble(saved.getString("stealPercentage", "0"));
}

}

任何帮助将不胜感激。它可能会变得如此简单,我最终会在脸上砸自己。无论如何要小心。 (同样当我输入它时,我不是通过sa将其提交为零通常我尝试sa为4而sb为2应为.500。

1 个答案:

答案 0 :(得分:1)

您是否已调试以逐步完成代码并在问题方法返回错误值之前确切了解值是什么?另外 - 我可能在这里,但整数除法将返回一个整数(你的预期值是.5,它将是0)。请参阅this问题。