nesting loops (that’s loops inside of loops. why not?)

Download Nesting Loops (That’s loops inside of loops. Why not?)

Post on 01-Jan-2016

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Nesting Loops(Thats loops inside of loops.Why not?)

  • Consider a line of asterisks:

    *********************...

    How can I do this in a program?rowcolumn

  • Consider a line of asterisks:*************final int numberCols = 20;

    for (int col=1; col

  • Say I want rows and rows of this line.******************************************...final int numberRows = 5;final int numberCols = 20;

  • //output all of the linesfor (int row=1; row
  • Say instead we want:* * * * * * * * ...Note space between asterisks.

  • for (int row=1; row
  • Say we want a checkerboard:for (int row=1; row
  • for (int row=1; row
  • final int width = 8;//output all of the rowsfor (int r=1; r
  • final int width = 8;//output all of the rowsfor (int r=1; r
  • final int width = 8;//output all of the rowsfor (int r=1; r
  • final int width = 8;boolean bumpOver = false;//output all of the rowsfor (int r=1; r
  • final int width = 8;boolean bumpOver = false;//output all of the rowsfor (int r=1; r
  • /* * file : CheckerBoard.java * author: george j. grevera, ph.d. * desc. : program to output a checkerboard (problem 4.1, p. 74. */public class CheckerBoard {

    public static void main ( String[] s ) {//declare variables & initialize i/o//ouput startup messageSystem.out.println( "here's the checkerboard:\n" );final int width = 8;boolean bumpOver = false;//output all of the rowsfor (int r=1; r

  • Lets draw a rectangle/box.

  • Generate a rectangle.*********** ** *
  • Generate a rectangle.*********** ** *
  • Generate a rectangle.*********** ** *
  • Generate a rectangle.*********** ** *
  • Generate a rectangle.*********** ** *
  • Generate a rectangle.*********** ** *
  • final int rows = 10;//define number of rowsfinal int cols = 20;//define number of columns//Output the first line (just like the last).for (int c=0; c
  • Repeatedly prompt until user enters something valid.final intminimum = 0;intinputValue = minimum-1;while (inputValue < minimum) {inputValue = in.nextInt();}

  • Repeatedly prompt until user enters something valid.final intminimum = 0;intinputValue = minimum-1;booleaninvalid = true;while (invalid) {inputValue = in.nextInt();if (inputValue >= minimum) {invalid = false;}}

  • Repeatedly prompt until user enters something valid.final intminimum = 0;intinputValue = minimum-1;booleanvalid = false;while (!valid) {inputValue = in.nextInt();if (inputValue >= minimum) {valid = true;}}

  • Shakespearean insultsp. 75 of Per Brinch Hansens Java book (if time permits).

    Thou goatish, hasty-witted ratsbane!Thou loggerheaded, doghearted dewberry!Thou churlish, tickle-brained pigeon-egg!

    Our task is to prompt the user for the number of these to output, and then output that number of insults.

  • Shakespearean insultsThougoatish,hasty-wittedratsbane!Thouloggerheaded,doghearteddewberry!Thouchurlish,tickle-brainedpigeon-egg!alwaysalwayspick onepick onepick one

  • Shakespearean insults

    Column 1Column 2Column 3artlessbeetle-headedbarnaclebootlessclay-brainedcoxcombchurlishdoghearteddewberrydissemblingfly-bittenfoot-lickererranthasty-wittedgudgeonfobbingidle-headedjoltheadgoatishrough-hewnloutimpertinentswag-belliedminnowjarringtickle-brainedpigeon-eggloggerheadedweather-bittenratsbane

  • Shakespearean insults algorithmPrompt user for number of insults.Read in the number of insults.Output that number of insults.Output Thou .Pick and output a word from column 1.Output ,.Pick and output a word from column 2.Output .Pick and output a word from column 3.Output ! w/ newline.

  • Shakespearean insults algorithm//Prompt user for number of insults.//Read in the number of insults.//Output that number of insults.//Output Thou .//Pick and output a word from column 1.//Output ,.//Pick and output a word from column 2.//Output .//Pick and output a word from column 3.//Output ! w/ newline.

  • Shakespearean insults algorithmScanner in = new Scanner( System.in );//Prompt user for number of insults.System.out.print( "Enter the number of insults: " );//Read in the number of insults.final int count = in.nextInt();

  • Shakespearean insults algorithm//Output that number of insults.for (int i=0; i
  • //Output that number of insults.for (int i=0; i
  • //Output that number of insults.Random rnd = new Random();for (int i=0; i
  • //Output that number of insults.Random rnd = new Random();for (int i=0; i
  • //Output that number of insults.Random rnd = new Random();for (int i=0; i
  • //Pick and output a word from column 1.switch (rnd.nextInt(10)+1) {case 1:System.out.print( artless)break;case 2:System.out.print( bootless);break;default:System.out.print( loggerheaded);break;} //end switchSystem.out.print( , );//Pick and output a word from column 2.switch (rnd.nextInt(10)+1) {case 1:System.out.print( beetle-headed)break;case 2:System.out.print( clay-brained);break;default:System.out.print( weather-bitten);break;} //end switchSystem.out.print( );//Pick and output a word from column 3.

  • //Output that number of insults.Random rnd = new Random();for (int i=0; i

Recommended

View more >