•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,608 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,255 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1198 | Replies: 10
![]() |
•
•
Join Date: Feb 2006
Posts: 21
Reputation:
Rep Power: 3
Solved Threads: 0
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.
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.
Here's the test appilcation.
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 AnalysisTestMy 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.
import java.util.Scanner;
public class Analysis
{
public void processExamResults()
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
// 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)
// 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;
else // else result is not 1, so
failures = failures + 1; // increment failures
// increment studentCounter so loop eventually terminates
studentCounter = studentCounter + 1;
} // end while
// 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 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
Now it will exit from loop if any body enters other then 1 or 2.
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} elsebreak;
•
•
Join Date: Feb 2006
Posts: 21
Reputation:
Rep Power: 3
Solved Threads: 0
I just replaced break with 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?
if (result == 1) // if result 1,
passes = passes + 1; // increment passes;
else if(result == 2) {
failures = failures + 1; // increment failures
} else
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 10:11 pm.
•
•
Join Date: Apr 2007
Posts: 14
Reputation:
Rep Power: 2
Solved Threads: 4
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
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
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,434
Reputation:
Rep Power: 11
Solved Threads: 295
@deng_cen please use tags to insert code into post. It is the hash"#" sign in toolbar above the text area for reply.
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
•
•
•
•
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
for(int count =1; count <= 10; count++)
{
if(count == 5)
continue;
//this meessage will not be printed if count == 5
System.out.printf("%d", count);
} Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,434
Reputation:
Rep Power: 11
Solved Threads: 295
as long you don't do it as the code from deng_cen shows you alright
if (result == 1) {// if result 1,
passes = passes + 1; // increment passes;
studentCounter = studentCounter + 1;
continue;
} there is no reason for continue, no other commands in the if/else statement after continue
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
•
•
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,434
Reputation:
Rep Power: 11
Solved Threads: 295
yes you have it there, appropriate increment inside if/else statement
So studentCounter will be incremented only if the mark is 1 or 2
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Publilius Syrus
(~100 BC)
If we helped you to solve your problem, answered your question please mark your post as SOLVED.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: Tracing through a recursion program.
- Next Thread: Barcode printing in Java



Linear Mode