| Author |
while loop
|
Jim Harvey
Greenhorn
Joined: Jun 08, 2007
Posts: 7
|
|
Can someone pls show me or get me started in the right direction for writing a while loop to perforem the following steps: 1. Prompt the user to input two integers: dirstNum and secondNum. 2. Output all results to a file, placing an appropriate lable between each section of output. 3. Output all odd numbers between firstNum and secondNum. 4. Output the sum of all even numbers between firstNum and secondNum. 5. Output the numbers and their square between firstNum and secondNum. 6. Output the sun of the squares of the odd numbers between firstNum and secondnum. Thank you very much/jrh
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9948
|
|
well, for starters, i wouldn't do this with a single while loop, but with at least four. to begin, i wouldn't even write a while loop. i'd start by writing a program that does #1. write it, compile it, test it, re-test it, and then test it some more. note that you may have to put in a little extra 'test-code', perhaps outputting the numbers to the screen to convince yourself you are really saving them somewhere. only when i was SURE that that worked, would i consider working on anything else. probably the next thing i'd do it output the two input numbers to a file. again, code, test, retest, etc, until i was sure THAT worked. THEN start with #3, then #4, etc. So, what do you know how to do? can you write any of it? do you know how to write, compile and run a program?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Jim Harvey
Greenhorn
Joined: Jun 08, 2007
Posts: 7
|
|
Fred, This is for you... let's see what you can do! */ public class Assignment1 { /** * @param args */ public static void main( String[] args ) { // Create the scanner object used for input Scanner input = new Scanner(System.in); // Get the first integer System.out.print("Please enter an integer: "); int firstNum = input.nextInt(); // The first int // Get the second integer. Ask for it again if necessary (if it is not // greater than the first integer). int secondNum = firstNum; // The second int while( secondNum <= firstNum ) { System.out .print("Please enter another integer that is greater than the first: "); secondNum = input.nextInt(); if( secondNum <= firstNum ) { System.out.println(secondNum + " is not greater than " + firstNum + ". Please try again." ); } } // Open up the file for writing PrintWriter writer = null; // Writer used to write to a file try { writer = new PrintWriter( new BufferedWriter( new FileWriter( "assignment1.txt" ))); // Output odd numbers outputOddNumbers( writer, firstNum, secondNum ); // Output the sum of all even numbers outputSumOfEvenNumbers( writer, firstNum, secondNum ); // Output the squares of all numbers outputSquares( writer, firstNum, secondNum ); // Output the sum of squares outputSumOfSquaresOfOddNumbers( writer, firstNum, secondNum ); } catch( IOException e ) { System.out.println( "There was a problem with the file: " + e.getMessage() ); } finally { if( writer != null ) { writer.close(); } } } /** * This will output all the odd numbers between firstNum (inclusive) and secondNum * (inclusive) to a file. * @param writer The writer object to which we print the results. * @param firstNum The first integer. * @param secondNum The second integer. */ private static void outputOddNumbers( PrintWriter writer, int firstNum, int secondNum ) { writer.println( "The following is a list of odd integers between " + firstNum + " and " + secondNum + "." ); writer.println( "-----------------------------------------------------------------" ); int startNum; // This will be the first odd number if( firstNum % 2 == 1 ) { // firstNum is odd startNum = firstNum; } else { // firstNum is even startNum = firstNum + 1; } // Start at the first odd number, and add 2 each time in a loop to get the next // odd number. Stop after we pass secondNum. for( int i = startNum; i <= secondNum; i += 2 ) { writer.println( i ); } // Add a blank line writer.println( "" ); } /** * This will output the sum of all the even numbers between firstNum (inclusive) and * secondNum (inclusive) to a file. * @param writer The writer object to which we print the results. * @param firstNum The first integer. * @param secondNum The second integer. */ private static void outputSumOfEvenNumbers( PrintWriter writer, int firstNum, int secondNum ) { writer.println( "The following is the sum of all even integers between " + firstNum + " and " + secondNum + "." ); writer.println( "-----------------------------------------------------------------" ); int startNum; // This will be the first even number if( firstNum % 2 == 0 ) { // firstNum is even startNum = firstNum; } else { // firstNum is odd startNum = firstNum + 1; } // Start at the first even number, and add 2 each time in a loop to get the next // even number. Stop after we pass secondNum. int sum = 0; // The sum of all even numbers for( int i = startNum; i <= secondNum; i += 2 ) { sum += i; } // Write the sum out writer.println( sum ); // Add a blank line writer.println( "" ); } /** * This will output the all the numbers, and their squares, between firstNum (inclusive) and * secondNum (inclusive) to a file. * @param writer The writer object to which we print the results. * @param firstNum The first integer. * @param secondNum The second integer. */ private static void outputSquares( PrintWriter writer, int firstNum, int secondNum ) { writer.println( "The following is the squares of all numbers between " + firstNum + " and " + secondNum + "." ); writer.println( "-----------------------------------------------------------------" ); // Output every number and their square. for( int i = firstNum; i <= secondNum; i++ ) { writer.println( i + "^2=" + ( i*i )); } // Add a blank line writer.println( "" ); } /** * This will output the sum of all the squares of the odd numbers between firstNum * (inclusive) and secondNum (inclusive) to a file. * @param writer The writer object to which we print the results. * @param firstNum The first integer. * @param secondNum The second integer. */ private static void outputSumOfSquaresOfOddNumbers( PrintWriter writer, int firstNum, int secondNum ) { writer.println( "The following is the sum of all the squares of odd integers between " + firstNum + " and " + secondNum + "." ); writer.println( "-----------------------------------------------------------------" ); int startNum; // This will be the first odd number if( firstNum % 2 == 1 ) { // firstNum is odd startNum = firstNum; } else { // firstNum is even startNum = firstNum + 1; } // Start at the first odd number, and add 2 each time in a loop to get the next // odd number. Stop after we pass secondNum. int sum = 0; // This is the sum of squares for( int i = startNum; i <= secondNum; i += 2 ) { sum += i * i; } // Output the sum writer.println( sum ); // Add a blank line writer.println( "" ); } }
|
 |
 |
|
|
subject: while loop
|
|
|