944,144 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2334
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
May 13th, 2007
0

Alright this loop problem is bugging me

Expand Post »
I can't figure out a way to have this program continue to loop if you press any number other than 1 or 2.



Here's the test appilcation.

Java Syntax (Toggle Plain Text)
  1. public class AnalysisTest
  2. {
  3. public static void main( String args[] )
  4. {
  5. Analysis application = new Analysis(); // create Analysis object
  6. application.processExamResults(); // call method to process results
  7. } // end main
  8.  
  9. } // end class AnalysisTest




My problem is in the class below.


This is what i have so far. Im thinking of putting this in there replace the if statement.


if ( result == 0 )
continue;
if ( result >= 3 )
continue;
if ( failures == 0 )
continue;
if ( failures >= 3 )
continue;

if ( result == 1 ) // if result 1,
passes = passes + 1; // increment passes;
else ( failures == 2 ) // else result is not 1, so
failures = failures + 1; // increment failures



I can't figure it out for some reason

I did this in BASIC and I can't remember how.



Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2. public class Analysis
  3. {
  4. public void processExamResults()
  5. {
  6. // create Scanner to obtain input from command window
  7. Scanner input = new Scanner( System.in );
  8.  
  9. // initializing variables in declarations
  10. int passes = 0; // number of passes
  11. int failures = 0; // number of failures
  12. int studentCounter = 1; // student counter
  13. int result; // one exam result (obtains value from user)
  14.  
  15. // process 10 students using counter-controlled loop
  16. while ( studentCounter <= 10 )
  17. {
  18. // prompt user for input and obtain value from user
  19. System.out.print( "Enter result (1 = pass, 2 = fail): " );
  20. result = input.nextInt();
  21.  
  22. // if...else nested in while
  23. if ( result == 1 ) // if result 1,
  24. passes = passes + 1; // increment passes;
  25. else // else result is not 1, so
  26. failures = failures + 1; // increment failures
  27.  
  28. // increment studentCounter so loop eventually terminates
  29. studentCounter = studentCounter + 1;
  30. } // end while
  31.  
  32. // termination phase; prepare and display results
  33. System.out.printf( "Passed: %d\nFailed: %d\n", passes, failures );
  34.  
  35. // determine whether more than 8 students passed
  36. if ( passes > 8 )
  37. System.out.println( "Raise Tuition" );
  38. } // end method processExamResults
  39.  
  40. } // end class Analysis
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stoney is offline Offline
21 posts
since Feb 2006
May 13th, 2007
0

Re: Alright this loop problem is bugging me

First thing you didn't do anything in your code so that it exit if anybody enter "result > 2" So it loops until 10 times and after that it exit.
Replace your existing code with this

if (result == 1) // if result 1,
passes = passes + 1; // increment passes;
else if(result == 2) {
// else result is not 1, so failures = failures + 1; // increment failures
} else
break;
Now it will exit from loop if any body enters other then 1 or 2.
Reputation Points: 10
Solved Threads: 3
Light Poster
lucky1981_iway is offline Offline
46 posts
since Apr 2007
May 13th, 2007
0

Re: Alright this loop problem is bugging me

Actually what I'm trying to figure out is how to keep the loop going even if you enter other numbers in.
Last edited by Stoney; May 13th, 2007 at 11:05 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stoney is offline Offline
21 posts
since Feb 2006
May 13th, 2007
0

Re: Alright this loop problem is bugging me

I just replaced break with continue.


Java Syntax (Toggle Plain Text)
  1. if (result == 1) // if result 1,
  2.  
  3. passes = passes + 1; // increment passes;
  4.  
  5. else if(result == 2) {
  6.  
  7. failures = failures + 1; // increment failures
  8.  
  9. } else
  10.  
  11. continue;


Your code you gave me somehow commented the failures = failures +1 so at first it wasnt incrementing that.


I think I got it. Right?
Last edited by Stoney; May 13th, 2007 at 11:11 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stoney is offline Offline
21 posts
since Feb 2006
May 14th, 2007
0

Re: Alright this loop problem is bugging me

hi,
Maybe this code can help you.

package com.prom1;
import java.util.Scanner;
public class AnalysisTest {
public static void main(String args[]) {
Analysis application = new Analysis(); // create Analysis object
application.processExamResults(); // call method to process results
} // end main
} // end class AnalysisTest
class Analysis {
public void processExamResults() {
// create Scanner to obtain input from command window
// initializing variables in declarations
int passes = 0; // number of passes
int failures = 0; // number of failures
int studentCounter = 1; // student counter
int result; // one exam result (obtains value from user)
Scanner input = new Scanner(System.in);
// process 10 students using counter-controlled loop
while (studentCounter <= 10) {
// prompt user for input and obtain value from user
System.out.print("Enter result (1 = pass, 2 = fail): ");
result = input.nextInt();
// if...else nested in while
if (result == 1) {// if result 1,
passes = passes + 1; // increment passes;
studentCounter = studentCounter + 1;
continue;
} else if (result == 2) {
// else result is not 1, so
failures = failures + 1; // increment failures
studentCounter = studentCounter + 1;
continue;
} else {
System.out.println("that is wrong! Please input number again!");
continue;
}
// increment studentCounter so loop eventually terminates
} // end while3
// termination phase; prepare and display results
System.out.printf("Passed: %d\nFailed: %d\n", passes, failures);
// determine whether more than 8 students passed
if (passes > 8)
System.out.println("Raise Tuition");
} // end method processExamResults
} // end class Analysis
Reputation Points: 30
Solved Threads: 4
Newbie Poster
deng_cen is offline Offline
14 posts
since Apr 2007
May 14th, 2007
0

Re: Alright this loop problem is bugging me

@deng_cen please use tags to insert code into post. It is the hash"#" sign in toolbar above the text area for reply.

Quote ...
The continue statement, when executed in a while, for or do...while, skips remaining statements in the loop body and process with next iteration of the loop. In while and do...while statements, the program evaluates the loop-ontinuation test immidiately after the continue statement executes. In a for statement, the increment expression executes, then the program evalues the loop-continuation test.
I hate to look into Deitel&Deitel book to get you proper explanation to make it clear as I may get on rants to explain what the continue is for. In your case continue is not neccesary as you do not have any commands after continue in your if/else checks so you can remove them. Example bellow will show when continue statement is usefull
Java Syntax (Toggle Plain Text)
  1. for(int count =1; count <= 10; count++)
  2. {
  3. if(count == 5)
  4. continue;
  5.  
  6. //this meessage will not be printed if count == 5
  7. System.out.printf("%d", count);
  8. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
May 14th, 2007
0

Re: Alright this loop problem is bugging me

So the way I have it. Is that ok?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stoney is offline Offline
21 posts
since Feb 2006
May 14th, 2007
0

Re: Alright this loop problem is bugging me

Click to Expand / Collapse  Quote originally posted by Stoney ...
So the way I have it. Is that ok?
as long you don't do it as the code from deng_cen shows you alright
Java Syntax (Toggle Plain Text)
  1. if (result == 1) {// if result 1,
  2. passes = passes + 1; // increment passes;
  3. studentCounter = studentCounter + 1;
  4. continue;
  5. }

there is no reason for continue, no other commands in the if/else statement after continue
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
May 14th, 2007
0

Re: Alright this loop problem is bugging me

I understand that but is there another way to do the problem without adding continue to it at all?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stoney is offline Offline
21 posts
since Feb 2006
May 14th, 2007
0

Re: Alright this loop problem is bugging me

yes you have it there, appropriate increment inside if/else statement

if (result == 1) {// if result 1,
passes = passes + 1; // increment passes;
studentCounter = studentCounter + 1;
} 
else if (result == 2) {// if result 2,
failures = failures + 1;
studentCounter = studentCounter + 1;
continue;
}

So studentCounter will be incremented only if the mark is 1 or 2
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Tracing through a recursion program.
Next Thread in Java Forum Timeline: Barcode printing in Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC