Lesson%20Set%207 - CS 1136 Lab 7 Continuation of loops...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 1136 Lab 7 Continuation of loops Carefully examine the program below and try to work out what occurs in the nested for loops. What do you predict the output will be? public class Lab7_Loops { Public static void main(String [] args) { Scanner keyboard = new Scanner(System.in()); int x, y; System.out.print(“Enter an integer in the range “ + “ 1- 20: “ ); x = keyboard.nextInt(); System.out.print(“Enter another integer in the range + “ 1- 20: “ ); y = keyboard.nextInt(); for (int i = 1; i <= y; i++) { for ( int j = 1; j <= x; j++) { System.out.print( „@‟ ); } System.out.println(); } System.exit(0); } Now enter it into your editor, compile, and run it. Was your prediction correct? If not, you need to practice more with nested for loops; you will find that this type of construct is very common in all programming languages. If you can grasp the logic above, you can apply that understanding to programs written in just about any programming language. Now make the following changes to the variable names: Replace: x‟s with num_of_columns y‟s with num_of_rows i‟s with row j‟s with column After making these changes examine the code again. It should be much easier to understand now. This example illustrates why you should always use descriptive names for your variables in your programs. Exercise 1:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Write a program that reads five integers from the keyboard. Each integer is between 1 and 10 ( you will need to validate the user ’s input ; if you reject the user’s input, you must re-read the input until a valid quantity is entered
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2008 for the course CS 1136 taught by Professor N/a during the Spring '08 term at University of Texas at Dallas, Richardson.

Page1 / 4

Lesson%20Set%207 - CS 1136 Lab 7 Continuation of loops...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online