mercredi 29 avril 2015

Error in logic for year comparison tool

I'm attempting to create a tool that compares the current year to the year that an event took place and then allows the user to guess how many years it has been since that event occurred. For example, the inaugural FRC Championship took place in 1992. The current year is 2015. The user guesses that it took place 23 years ago, which is correct.

So, in my mind the code would look like this...

  public class Credits extends Activity {
    int inaugural = 1992;
    int guess;
    int differenceInYears;
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credits);
        final EditText yearGuess=(EditText)findViewById(R.id.txtYearGuess);
        Button go = (Button)findViewById(R.id.btnCalculate);
        final TextView result = ((TextView)findViewById(R.id.txtResult));
        go.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int userGuess= Integer.parseInt(yearGuess.getText().toString());
                differenceInYears = year - inaugural;
                output = userGuess - differenceInYears;

                if (output < 1) {
                    result.setText("Guess again! You guessed too low!");
                }

                else if (output == 1) {
                    result.setText("You're REALLY close!");

                }

                else if (output == -1) {
                    result.setText("You're REALLY close!");

                }

                else if (output > 1) {
                    result.setText("Guess again! You guessed too high!");

                }

                else {
                    result.setText("Good job! You're an FRC Genious!");

                }
            }

        });
     }
    }

...however, the values continue to come out wrong when tested. What am I missing here? Is there something wrong with my math? or code? or both?

Aucun commentaire:

Enregistrer un commentaire