Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By Wnt2bsleepin
  • 1 Post By brynpttrsn

Thread: Why is this code never reached?

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Why is this code never reached?

    specs: I'm creating a learning tool for kids that asks a question, what is x times y? where x and y are random integers.

    if they score 75% correct then they move up a level otherwise not.

    the problem I am having is that the if statement in checkComplete never works (possibly means correct and incorrect never reach 10)

    I have tried a few fixes.

    1) I realised that when I called the method again after a correct answer it will reassign correct and incorrect as 0, I did put a while(true) loop to block of the code from those declarations but this didnt work (I have taken the while loop out of the code because it messes up the indentation for you guys)

    2) does if( checkComplete(correct, incorrect) == true) execute the method or just check the return value? I dont actually know... this is why I tried adding checkComplete(correct, incorrect) above it incase it doesnt execute in the if statement

    can you please explain whether a method does execute when put in a conditional statement?

    anyway, that didnt work either

    heres me code

    Java Code:
    package chapter_vi;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class ComputerAssistedInstructionMonitoringStudentPerformance_6_37 
    {
    	public static boolean checkComplete(int correct, int incorrect)
    	{
    		boolean complete = true;
    		
    		if( correct + incorrect == 10 )
    		{
    			System.out.println("You have scored " + correct + " correct, and " + incorrect +
    					" incorrect.");
    			
    			if (correct >= (10/100) * 75)
    			{
    				System.out.println("Congratulations, you are ready to go to the next level!");
    			}
    			else
    			{
    				System.out.println("Please ask your teacher for extra help");
    			}
    		}
    
    		return complete;
    	}
    	public static void question()
    	{
    		Scanner input = new Scanner(System.in);
    		Random randomNumbers = new Random();
    		
    		int random1 = randomNumbers.nextInt(11);
    		int random2 = randomNumbers.nextInt(11);
    		int answer;
    		int result = (random1 * random2);
    		int correct = 0;
    		int incorrect = 0;
    		
    		System.out.println("Lets play multiplication");
    		System.out.println("What is " + random1 + " times " + random2 + "?");
    		answer = input.nextInt();
    		
    		while( answer != result)
    		{
    			switch(1 + randomNumbers.nextInt(4))
    			{
    				case 1:
    					System.out.println("No. Please try again.");
    					break;
    				case 2:
    					System.out.println("Wrong. Try once more.");
    					break;
    				case 3:
    					System.out.println("Don't give up!");
    					break;
    				case 4:
    					System.out.println("No. Keep trying.");
    			}
    			incorrect++;
    			if( checkComplete(correct, incorrect) == true)
    				{
    					question();
    				}
    			
    			System.out.println("What is " + random1 + " x " + random2 + "?");
    			answer = input.nextInt();
    		}
    
    		switch(1 + randomNumbers.nextInt(4))
    		{
    			case 1:
    				System.out.println("Very good!");
    				break;
    			case 2:
    				System.out.println("Excellent!");
    				break;
    			case 3:
    				System.out.println("Nice work");
    				break;
    			case 4:
    				System.out.println("Keep up the good work");
    		}
    		correct++;
    
    		if( checkComplete(correct, incorrect) == true)
    		{
    			question();
    		}
    		
    		System.out.println();
    		
    	}
    	
    	public static void main(String[] args)
    	{
    		question();
    	}
    }
    what do you guys think?

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Why is this code never reached?

    I did notice that CheckComplete never returns false if it should.
    EscSequenceAlpha likes this.

  3. #3
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Why is this code never reached?

    Yes methods do execute no matter where they are called in the code. And as Wnt pointed out you should have complete be set to false somewhere in that method.
    Last edited by brynpttrsn; 05-07-2012 at 09:40 AM.
    EscSequenceAlpha likes this.

  4. #4
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why is this code never reached?

    Quote Originally Posted by brynpttrsn View Post
    Yes methods do execute no matter where they are called in the code. And as Wnt pointed out you should have complete be set to false somewhere in that method.
    Really? ok I'll try that... but I thought boolean variables once declared would be given the value of false like strings are null, I thought I read taht somewhere or am I confusing things again.

  5. #5
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Why is this code never reached?

    Java Code:
    public static boolean checkComplete(int correct, int incorrect)
        {
            boolean complete = true;
             
            if( correct + incorrect == 10 )
            {
                System.out.println("You have scored " + correct + " correct, and " + incorrect +
                        " incorrect.");
                 
                if (correct >= (10/100) * 75)
                {
                    System.out.println("Congratulations, you are ready to go to the next level!");
                }
                else
                {
                    System.out.println("Please ask your teacher for extra help");
                }
            }
     
            return complete;
        }
    In this method you set complete to true. Its not set to false anywhere, so the only possible outcome of this method is returning true.

  6. #6
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why is this code never reached?

    Quote Originally Posted by EscSequenceAlpha View Post
    Really? ok I'll try that... but I thought boolean variables once declared would be given the value of false like strings are null, I thought I read taht somewhere or am I confusing things again.
    Sorry! I forgot what my code was... yes I should move the boolean variable after the if statement, so its false unless 10 answers.

    I did this and now it works as it should, thank you guys. and I learned methods do execute even as an argument in a conditional so I took the call out before it

    thanks

  7. #7
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why is this code never reached?

    also a side question... was it appropriate to use a while in the question method to prevent reinitialisation when method recalled

    or would it be more natural to have those variables belong as static class variables and then I wouldnt need a while(true) loop right?

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why is this code never reached?

    This isn't related to your problem but you'd better make your number of questions a multiple of four otherwise it's impossible to have a score of exactly 75% ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why is this code never reached?

    I saw that too, but the task actually stated 10 questions with 75% - odd I agree.

  10. #10
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why is this code never reached?

    Ok almost done, just run into one problem

    additions
    + added a difficulty feature, the initial difficulty is set, and if they score above 75% the difficulty increases

    problem
    the difficulty never seems to increase. after scoring > 75%

    [CODE]
    package chapter_vi;

    import java.util.Random;
    import java.util.Scanner;

    public class ComputerAssistedInstructionDifficultyLevels_6_38
    {
    static Scanner input = new Scanner(System.in);

    static int difficulty;

    public static boolean checkComplete(int correct, int incorrect)
    {
    boolean complete = false;

    if( correct + incorrect == 10 )
    {
    System.out.println("You have scored " + correct + " correct, and " + incorrect +
    " incorrect.");

    complete = true;

    if (correct >= (10/100) * 75)
    {
    System.out.println("Congratulations, you are ready to go to the next level!");
    difficulty++;
    }
    else
    {
    System.out.println("Please ask your teacher for extra help");
    }
    }

    return complete;
    }
    public static void question(int difficulty)
    {
    Random randomNumbers = new Random();

    int answer;
    int random1 = 0;
    int random2 = 0;
    int result;
    int correct = 0;
    int incorrect = 0;

    while(true)
    {

    if( difficulty == 1)
    {
    random1 = randomNumbers.nextInt(11);
    random2 = randomNumbers.nextInt(11);
    }
    else if( difficulty == 2)
    {
    random1 = randomNumbers.nextInt(101);
    random2 = randomNumbers.nextInt(101);
    }
    else // difficulty 3 (>= 3)
    {
    random1 = randomNumbers.nextInt(1001);
    random2 = randomNumbers.nextInt(1001);
    }

    result = (random1 * random2);

    System.out.println("Lets play multiplication");
    System.out.println("What is " + random1 + " times " + random2 + "?");
    answer = input.nextInt();

    while( answer != result )
    {

    switch(1 + randomNumbers.nextInt(4))
    {
    case 1:
    System.out.println("No. Please try again.");
    break;
    case 2:
    System.out.println("Wrong. Try once more.");
    break;
    case 3:
    System.out.println("Don't give up!");
    break;
    case 4:
    System.out.println("No. Keep trying.");
    }

    incorrect++;

    if( checkComplete(correct, incorrect) == true)
    {
    question(difficulty);
    }

    System.out.println("What is " + random1 + " times " + random2 + "?");
    answer = input.nextInt();
    }

    switch(1 + randomNumbers.nextInt(4))
    {
    case 1:
    System.out.println("Very good!");
    break;
    case 2:
    System.out.println("Excellent!");
    break;
    case 3:
    System.out.println("Nice work");
    break;
    case 4:
    System.out.println("Keep up the good work");
    }
    correct++;

    if( checkComplete(correct, incorrect) == true)
    {
    question(difficulty);
    }

    System.out.println();
    }
    }

    public static void main(String[] args)
    {
    System.out.println("What level of difficulty would you like? (1, 2, 3)");
    difficulty = input.nextInt();
    question(difficulty);
    }
    }

    [\CODE]

    can you spot the problem? I cant

    regards,
    ESA

  11. #11
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why is this code never reached?

    Java Code:
    package chapter_vi;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class ComputerAssistedInstructionDifficultyLevels_6_38 
    {
    	static Scanner input = new Scanner(System.in);
    	
    	static int difficulty;
    	
    	public static boolean checkComplete(int correct, int incorrect)
    	{
    		boolean complete = false;
    		
    		if( correct + incorrect == 10 )
    		{
    			System.out.println("You have scored " + correct + " correct, and " + incorrect +
    					" incorrect.");
    			
    			complete = true;
    			
    			if (correct >= (10/100) * 75)
    			{
    				System.out.println("Congratulations, you are ready to go to the next level!");
    				difficulty++;
    			}
    			else
    			{
    				System.out.println("Please ask your teacher for extra help");
    			}
    		}
    
    		return complete;
    	}
    	public static void question(int difficulty)
    	{
    		Random randomNumbers = new Random();
    		
    		int answer;
    		int random1 = 0;
    		int random2 = 0;
    		int result;
    		int correct = 0;
    		int incorrect = 0;
    		
    		while(true)
    		{
    		
    		if( difficulty == 1)
    		{
    			random1 = randomNumbers.nextInt(11);
    			random2 = randomNumbers.nextInt(11);
    		}
    		else if( difficulty == 2)
    		{
    			random1 = randomNumbers.nextInt(101);
    			random2 = randomNumbers.nextInt(101);
    		}
    		else // difficulty 3 (>= 3)
    		{
    			random1 = randomNumbers.nextInt(1001);
    			random2 = randomNumbers.nextInt(1001);
    		}
    		
    		result = (random1 * random2);
    		
    			System.out.println("Lets play multiplication");
    			System.out.println("What is " + random1 + " times " + random2 + "?");
    			answer = input.nextInt();
    		
    			while( answer != result )
    			{
    				
    				switch(1 + randomNumbers.nextInt(4))
    				{
    					case 1:
    						System.out.println("No. Please try again.");
    						break;
    					case 2:
    						System.out.println("Wrong. Try once more.");
    						break;
    					case 3:
    						System.out.println("Don't give up!");
    						break;
    					case 4:
    						System.out.println("No. Keep trying.");
    				}
    			
    			incorrect++;
    			
    			if( checkComplete(correct, incorrect) == true)
    				{
    					question(difficulty);
    				}
    			
    			System.out.println("What is " + random1 + " times " + random2 + "?");
    			answer = input.nextInt();
    			}
    			
    			switch(1 + randomNumbers.nextInt(4))
    			{
    				case 1:
    					System.out.println("Very good!");
    					break;
    				case 2:
    					System.out.println("Excellent!");
    					break;
    				case 3:
    					System.out.println("Nice work");
    					break;
    				case 4:
    					System.out.println("Keep up the good work");
    			}
    			correct++;
    
    			if( checkComplete(correct, incorrect) == true)
    			{
    				question(difficulty);
    			}
    		
    			System.out.println();
    			}
    	}
    	
    	public static void main(String[] args)
    	{
    		System.out.println("What level of difficulty would you like? (1, 2, 3)");
    		difficulty = input.nextInt();
    		question(difficulty);
    	}
    }
    SORRY I did quick reply because reply to thread didnt work for minute and quick reply doesnt have code inserts

  12. #12
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why is this code never reached?

    I can start with difficult == 3 if In put it in at the start but it doesnt increase if I input 1 or 2 at the start

    the "Congratulations, you are ready to go to the next level!" prints if you score 75% but the difficulty++ doesnt seem to work

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why is this code never reached?

    Quote Originally Posted by EscSequenceAlpha View Post
    SORRY I did quick reply because reply to thread didnt work for minute and quick reply doesnt have code inserts
    You can always simply type the characters [code] ... [/code].

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Why is this code never reached?

    Here's one problem:

    Java Code:
    if (correct >= (10/100) * 75)
    In integer math, 10/100 = 0. So the statement is equivalent to:

    Java Code:
    if (correct >= 0)
    I don't think that's what you wanted.
    Get in the habit of using standard Java naming conventions!

  15. #15
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: Why is this code never reached?

    Java Code:
    static int difficulty;
    Java Code:
    public static void question(int difficulty)
    There are two variables that are called difficulty, one global one, and the one passed into the questions() method.

    If you changed
    Java Code:
                if( checkComplete(correct, incorrect) == true)
                {
                    question(difficulty);
                }
    to
    Java Code:
                if( checkComplete(correct, incorrect) == true)
                {
                    question(this.difficulty);
                }
    It would work, but this is a quick fix and probably isn't the best thing to do.
    You should probably only have one difficulty variable.

Similar Threads

  1. Replies: 1
    Last Post: 09-28-2011, 09:33 AM
  2. reached end of file while parsing- using bluej
    By prendergast36 in forum New To Java
    Replies: 1
    Last Post: 09-08-2011, 09:00 PM
  3. Log out when the maxInactiveTime is reached.
    By uncleshah in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-24-2008, 12:19 AM
  4. intimating file(s) have reached/copied in directory
    By ashu261 in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 06:24 PM
  5. signaling file(s) have reached in directory
    By ashu261 in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •