hi everyone

i am trying to do some subtraction in a java program . the rules stated that generated numbers from 1 to 9 and the first number must be greater than the second number.

I try using the largest and smallest codes but the result i get is that zero which is not supposed to happen. can you tell me what to do. here is my codes and the output
the first round of the game is supposed to be four rounds, it should only repeated the question only if the user get the answer wrong, and it should be repeated until the correct answer is enter but it is repeating when the user enter the correct answer which should not be and it is not giving u the four questions, it is just jumping straight to round 2. what is wrong.

   char choice;
        char ans;
        int i = 0, wrongcount=0;
        int incorrectans = 0, correctans = 0,  score = 0, totalscore = 0, sum_neg =0;
        int sum_pos = 0,  tries = 0;
        String playername;
        int rand, rand2, playerage;
        int question_count =0;
        int max_num = 0, num1 = 0;
        int min_num = 0, num2 = 0;
        //initializing the variable that will be used during the course of the game

        System.out.println("Welcome to Summer's Eve Multiple Choice Math Program");
        System.out.println("The game consist of 3 round with 6 level");
        System.out.println("The age limit of this game is 8 - 13 years ");
        System.out.println("The player wins the game if all questions are answerred correctly");
        System.out.println("The player loses the game if they run out of chances");
        System.out.println("Each player has 4 chances");
        System.out.println("Each correct answer is rewarded with a maximum of 650 points");
        System.out.println("Each incorrect answer will lose half of the alloted point given ");
        System.out.println("A report is submitted at the end of the game");
        System.out.println("A player only win the game if all 10 question are answered correctly");
        System.out.println("A player loses the game if the age is entered incorrectly or if the player ran out of the chances given");
        System.out.println("okay students, I hope that I have made myself very clear");
        //indicating the rules and regulations of the game 


        System.out.println("                            ");


        System.out.println("******************welcome to the round 1****************");


        System.out.println("Hi what is your name");
        playername = userinput.next();
        System.out.println(playername+" your name is very unique, my dearest");
        System.out.println(playername+" how old are you");
        playerage = userinput.nextInt();
        //asking for the user name and age

        if(playerage < 8 || playerage > 13)
        {
            System.out.println("you have entered an invalid age");
            System.exit(0);
            //this is used for the userage. the game will exit if the wrong age is entered
        }

        else 
        {
            System.out.println(playername+" come let's play");
            //this is indicating   that the user can play after inputting the correct age
        }

        i=1;
        while(i <4)
        {
            //get random numbers
            if(i ==1 || i == 2)//come up with a model boolean explression that reflect in the if statement
            {
                rand  = 1 + randobj.nextInt(25);// numbers to be randomy generated
                rand2 = 1 + randobj.nextInt(8);//to hold the second set of random numbers
                correctans = rand + rand2;// this is holding all the correct answer
                incorrectans = 25 + randobj.nextInt(50);//this is to hold al the incorrect answer

                //letting the user know what rounds and the level they will be starting on
                System.out.println(playername+" round 1 "+ " level "+ i+"question "+i+" you are required to match the letter to the correct answer");
                System.out.println(playername+" please add "+rand + " + "+ rand2);
                //asking the required questions

                //get the multiple choice answers
                System.out.println("A:"+correctans);
                System.out.println("B:"+incorrectans);
                choice = userinput.next().charAt(0);

                //checking to see if the choice chosen is the correct answer
                if(choice == 'A' || choice == 'a')//this is not restricted any one character
                {
                    System.out.println(playername+" awwwwsome well done,  correct is right "+ " excellent!!!!");
                    score = score + 650;
                    System.out.println("round 1");
                    System.out.println("level "+ i);
                    totalscore += score;
                    System.out.println("the total score is "+ totalscore);

                    //indicating that the user has enter the correct answer
                }
                else if(choice == 'B' || choice == 'b')
                {
                    System.out.println(playername+" aahhhh!! you are so wrong "+ " this doesn't look too good");
                    score -= score -225;
                    System.out.println("round 1");
                    System.out.println("level "+ i);
                    totalscore -= score;
                    i++;
                    //this indicate that the user answer the incorrect answer

                }

                if(wrongcount < 4)
                {
                    System.out.println(playername+"round 1 "+ "level "+i+ "question "+i+" you are asked to match the letter to the correct answer");
                    System.out.println(playername+"please add "+ rand+ " + "+ rand2);

                    System.out.println("A:"+correctans);
                    System.out.println("B:"+incorrectans);
                    choice = userinput.next().charAt(0);

                    if(choice == 'A' || choice == 'a')
                    {
                        System.out.println(playername+" you are so right "+ "excellent job, keep it up");
                        score += score + 650;
                        System.out.println(" round 1");
                        System.out.println("level "+i);
                        System.out.println("the total score is "+ totalscore);

                    }
                    else if(choice == 'B'|| choice == 'b')
                    {
                        System.out.println(playername+"ah no please dont say so, that is incorrect "+ " please try again");
                        score -= score -225;
                        System.out.println("round 1");
                        System.out.println("level "+ i);
                        System.out.println("the the score is "+ totalscore);
                        i++;
                    }


                }
                 else if(wrongcount == 4)
                {
                    System.out.println("too many invalid tries");
                }




                System.out.println("***************************welcome to round 2*******************************");

                i =1;
                while(i < 4)
                {
                   if (i == 1 || i == 2)
                {
                    num1 = 1 + randobj.nextInt(25);
                    num2 = 1 + randobj.nextInt(8);
                    correctans = max_num - min_num;
                    incorrectans = 25 + randobj.nextInt(59);

                    if(num1 > max_num)
                    {
                        max_num = num1;
                    }
                    else if(num2 < min_num)
                    {
                        min_num = num2;
                    }


                     System.out.println(playername+"round 2 "+ " level "+ i+" question "+i+ " you are required to match the letter with the ccorresponding answer");
                     System.out.println(playername+" please subtract "+ max_num+ " - "+min_num);

                     System.out.println("A:"+incorrectans);
                     System.out.println("B:"+correctans);
                     choice = userinput.next().charAt(0);

                if(choice == 'A' || choice == 'a')//this indication that any letter can be entered.
                {
                    System.out.println(playername+"oh no please, dont tell me you have answered incorrectly "+ " not good");
                    score -= score - 225;
                    System.out.println("round 2");
                    System.out.println("level "+ i);
                    System.out.println("the total score is "+ totalscore);

                }
                else if(choice == 'B'|| choice == 'b')
                {
                    System.out.println(playername+" good joh, correct is right again "+ " keep up the good work");
                    score += score + 650;
                    System.out.println("round 2");
                    System.out.println("level "+i);
                    System.out.println("the total score is "+totalscore);
                    i++;
                }

                }

                }

             here is the output
             The game consist of 3 round with 6 level
The age limit of this game is 8 - 13 years 
The player wins the game if all questions are answerred correctly
The player loses the game if they run out of chances
Each player has 4 chances
Each correct answer is rewarded with a maximum of 650 points
Each incorrect answer will lose half of the alloted point given 
A report is submitted at the end of the game
A player only win the game if all 10 question are answered correctly
A player loses the game if the age is entered incorrectly or if the player ran out of the chances given
okay students, I hope that I have made myself very clear

******************welcome to the round 1****************
Hi what is your name
akeda
akeda your name is very unique, my dearest
akeda how old are you
10
akeda come let's play
akeda round 1  level 1question 1 you are required to match the letter to the correct answer
akeda please add 18 + 3
A:21
B:41
b
akeda aahhhh!! you are so wrong  this doesn't look too good
round 1
level 1
akedaround 1 level 2question 2 you are asked to match the letter to the correct answer
akedaplease add 18 + 3
A:21
B:41
a
akeda you are so right excellent job, keep it up
 round 1
level 2
the total score is -225
***************************welcome to round 2*******************************
akedaround 2  level 1 question 1 you are required to match the letter with the ccorresponding answer
akeda please subtract 14 - 0
A:43
B:0
BUILD STOPPED (total time: 1 

Recommended Answers

All 4 Replies

Around lines 144-146 you seem confused about what variables you are using.

hi james

I have since corrected it and I am still getting the same result. the rules of the games say that it should never generate zero and for some reason it is doing same.

I'm not a mind reader. You have a problem in a version of your code that you hven't shown. And anyway, I'm not going to read any more of your code unless you indent it correctly - it's just too hard to follow all those nested whiles & ifs (eg lines 140-142... what's that all about?). It will be easier ro read if you indent according to the Sun/Oracle preferred system eg

if (blah) {
   stuff
} else {
   other stuff
}

rather than having all those lines with just a single curly open bracket on them that make for uneccessary vertical scrolling to see all the code.

i got the subtracting in java to work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.