ok guys.... i have a multiple choice scenario to deal with.... my problem is in the division....both my
random numbers have been declared as integers.. this caused the problem of the answer being
truncated and only the whole number portion being displayed... i tried to cast one as a double but
it's not working...any suggestions....

for(counter = 1; counter <= 2; counter++) {


            responseAns = false; correct = false;
            Random_num1 = 1 + r.nextInt(25);

            //Makes sure our second number is not bigger than our first number
            do{
                Random_num2 = 1 + r.nextInt(9);
            }
            while(Random_num1 < Random_num2);


            correct_ans = Random_num1 / Random_num2;
            incorrect_ans = correct_ans + Random_num2;

            System.out.print("Question# " + counter + ":   ");
            System.out.println(Random_num1 + " / " + Random_num2 + " = ");

            int position = r.nextInt(2);
            if(position == 0)
            {
                A = correct_ans;
                B = incorrect_ans;
            }
            else {
                A = incorrect_ans;
                B = correct_ans;
            }

            // difference = input.nextInt();
            while (!correct) {
                System.out.println("A " + A);
                System.out.println("B " + B);

                String response = input.next().toUpperCase();
                if(position == 0)
                {
                    if(response.equals("A"))
                    {
                        responseAns = true;
                    }
                }
                else
                {
                    if(response.equals("B"))
                    {
                        responseAns = true;
                    }
                }

                if (responseAns) {
                    System.out.println("CORRECT!  Great Work " + name);
                    count_correct_ans++;
                    points++;
                    correct=true;
                } //END OF INNER IF
                else {
                    chances--;
                    if(chances == 1){                         
                       display_chances = "chance"; 
                    }
                    else if(chances == 0){
                        System.out.println("Sorry you have lost. You do not have any more chances");
                        System.out.println("");
                        System.out.println("PLAYER'S NAME: " + name);
                        System.out.println("PLAYER'S AGE: " + age);
                        System.out.println("QUESTIONS ANSWERED: " + count_correct_ans);
                        System.out.println("POINTS MADE: " + points);
                        System.out.println("");
                        System.out.println("END OF GAME");
                        System.out.println("++++++++++++++++++++++++++++++++++++++++++++");
                        System.exit(0);
                    }
                    System.out.println("INCORRECT! Try again, " + chances + " " + display_chances +  " Remaining");
                    System.out.println(Random_num1 + " / " + Random_num2 + " = ");
                    count_incorrect_ans++;
                    correct=false;
                } // END OF INNER ELSE  
            }// END OF WHILE LOOP
        } // END OF FOR LOOP

that's the working code with that displays only the whole number portion of the answer

To get a floating point division you have to have one or both of the operands to be floating point before you do the division. You can make one or both operands double, or use this little "trick"
int a,b;
double ans = a/(b*1.0); // forces dividend to double precision before the divison

Also, something that is better to start early with, it'll make your code shorter, more efficiƫnt and easier to maintain later on:

String response = input.next().toUpperCase();
                if(position == 0)
                {
                    if(response.equals("A"))
                    {
                        responseAns = true;
                    }
                }
                else
                {
                    if(response.equals("B"))
                    {
                        responseAns = true;
                    }
                }

This is quite a lot of code for a simple test. Firstly, going for "toUpperCase" is a bit 'dodgy', especially since the String class provides an alternative way to handle the "a" equals "A" issue.

String response = input.next();
                if(position == 0)
                {
                    if(response.equalsIgnoreCase("A"))
                    {
                        responseAns = true;
                    }
                }
                else
                {
                    if(response.equalsIgnoreCase("B"))
                    {
                        responseAns = true;
                    }
                }

Not just that, but the above can be simplified quite a bit:

String response = input.next().toUpperCase();
responseAns = (position == 0 && response.equalsIgnoreCase("A")) || (position != 0 && response.equalsIgnoreCase("B");

This may seem 'not relevant', but when debugging, going through 30 lines of code, is always easier compared to going through 75.

Seeing as you don't actually do anything with responseAns later on, you may just as well drop the variable alltogether:

String response = input.next().toUpperCase();
    if ( (position == 0 && response.equalsIgnoreCase("A")) || (position != 0 && response.equalsIgnoreCase("B")){
        System.out.println("CORRECT!  Great Work " + name);
                    count_correct_ans++;
                    points++;
                    correct=true;
    } else{ // ....
   }

If you do want to store the boolean somewhere, store it immediately in the 'correct' variable.
Especially debugging an iteration can be a pain, when you have a lot of code in there that doesn't have to be iterated over. An example of how you could refactor it, is putting all the logic of a success outside of the loop, same with the logic for chances == 0

String response = input.next().toUpperCase();
    correct = (position == 0) && response.equalsIgnoreCase("A")) || (position != 0 && response.equalsIgnoreCase("B"));
    if ( correct ){
        count_correct_ans++;  
        points++;
        break;
    } else{ 
    chances--;
                if(chances == 1){                         
                   display_chances = "chance"; 
                }
                else if(chances == 0){
                    break;
                }
                System.out.println("INCORRECT! Try again, " + chances + " " + display_chances +  " Remaining");
                System.out.println(Random_num1 + " / " + Random_num2 + " = ");
                count_incorrect_ans++;
                //correct=false; -> There is no need of this, since the value is already false.
   } // END OF INNER ELSE  
        }// END OF WHILE LOOP
    if ( correct ){
   System.out.println("CORRECT!  Great Work " + name); 
   }
   else{
   System.out.println("Sorry you have lost. You do not have any more chances");
                    System.out.println("");
                    System.out.println("PLAYER'S NAME: " + name);
                    System.out.println("PLAYER'S AGE: " + age);
                    System.out.println("QUESTIONS ANSWERED: " + count_correct_ans);
                    System.out.println("POINTS MADE: " + points);
                    System.out.println("");
                    System.out.println("END OF GAME");
                    System.out.println("++++++++++++++++++++++++++++++++++++++++++++");
                    }
   } // END OF FOR LOOP

                    System.exit(0);

Edited 11 Months Ago by stultuske

Wow...Thanks a million guys. I will check your suggestions and attempt implementations of them in a bit and let you know what the outcome was.....

Again,
Thanks

This article has been dead for over six months. Start a new discussion instead.