{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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:
Image of page 1

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

View Full Document Right 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
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern