Results 1 to 15 of 15
Thread: Why is this code never reached?
- 05-07-2012, 03:54 AM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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
what do you guys think?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(); } }
- 05-07-2012, 03:59 AM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Why is this code never reached?
I did notice that CheckComplete never returns false if it should.
- 05-07-2012, 04:13 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
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.
- 05-07-2012, 06:58 PM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
- 05-07-2012, 07:01 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: Why is this code never reached?
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.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; }
- 05-07-2012, 07:02 PM #6
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Why is this code never reached?
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
- 05-07-2012, 07:50 PM #7
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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?
- 05-07-2012, 08:01 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,376
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-07-2012, 08:10 PM #9
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Why is this code never reached?
I saw that too, but the task actually stated 10 questions with 75% - odd I agree.
- 05-07-2012, 08:26 PM #10
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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
- 05-07-2012, 08:28 PM #11
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Why is this code never reached?
SORRY I did quick reply because reply to thread didnt work for minute and quick reply doesnt have code insertsJava 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); } }
- 05-07-2012, 08:38 PM #12
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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
- 05-07-2012, 09:16 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,376
- Blog Entries
- 7
- Rep Power
- 17
- 05-08-2012, 03:36 AM #14
Re: Why is this code never reached?
Here's one problem:
In integer math, 10/100 = 0. So the statement is equivalent to:Java Code:if (correct >= (10/100) * 75)
I don't think that's what you wanted.Java Code:if (correct >= 0)
Get in the habit of using standard Java naming conventions!
- 05-08-2012, 05:22 AM #15
Member
- Join Date
- Sep 2011
- Posts
- 59
- Rep Power
- 0
Re: Why is this code never reached?
Java Code:static int difficulty;
There are two variables that are called difficulty, one global one, and the one passed into the questions() method.Java Code:public static void question(int difficulty)
If you changed
toJava Code:if( checkComplete(correct, incorrect) == true) { question(difficulty); }
It would work, but this is a quick fix and probably isn't the best thing to do.Java Code:if( checkComplete(correct, incorrect) == true) { question(this.difficulty); }
You should probably only have one difficulty variable.
Similar Threads
-
Error: reached end of file while parsing
By iswan in forum JDBCReplies: 1Last Post: 09-28-2011, 09:33 AM -
reached end of file while parsing- using bluej
By prendergast36 in forum New To JavaReplies: 1Last Post: 09-08-2011, 09:00 PM -
Log out when the maxInactiveTime is reached.
By uncleshah in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-24-2008, 12:19 AM -
intimating file(s) have reached/copied in directory
By ashu261 in forum Threads and SynchronizationReplies: 1Last Post: 04-23-2008, 06:24 PM -
signaling file(s) have reached in directory
By ashu261 in forum Advanced JavaReplies: 0Last Post: 02-04-2008, 07:45 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks