Control -  Loops


Control - nested loops

ForExamples.java

Deitel 5


Classwork


Create a project and in it a class with a main. 

We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste

import java.util.Scanner;
As the first line inside your main method, paste
Scanner scan = new Scanner(System.in);
In order to read from the user, we use the Scanner like this:
int x = scan.nextInt(); // read an int
double d = scan.nextDouble(); // read a double
String s = scan.next(); // read a String

Part A

Do the following in the main.

 Using a for loop with a while loop inside, for each number from 1 to 10 ask the user what the square of that number is, and tell them they are wrong until they give the correct value.  (For testing, you may want to set the for loop to run a smaller number of times, then set it to 10 when you know it works)

 Using nested for loops,  print a table of sums from 0+0 up to 5 + 5, i.e.

0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10

Credit will only be given if you generate all of this with nested for loops. 

Think about how you will get the values you need to print; you want 0+0, 0+1, 0+2, etc on the first row, then 1+0, 1+1, 1+2 etc on the second row.  If you are using nested loops, what variables do you automatically already have that go through exactly those combinations?

Within each row you are printing all on one line, but you want a new line at the end of each row. Think about the difference between System.out.print and System.out.println, and about what goes inside which loop

(hint: the whole point of loops is to make things happen multiple times, you only need two for loops to do this, one inside the other)


Part B 

 

In the same main (or you may add another class with main if you prefer; if so, give them names so it is clear which is which), use a while loop to let the user enter numbers, with a sentinel value  -989 to quit. 

You can assume that all the numbers are between (inclusive) 0 and 1000, so no number will be smaller than 0 or larger than 1000. 

At the end, report to the user what the largest and smallest numbers entered were.   (Remember that the sentinel value should NOT count as the largest or smallest).

In addition to a variable for the user input, you will need a variable to store the largest number, and a variable to store the smallest. Think about what these numbers should start as, so you don't accidentally choose too big or small a number to start.

You do not need to store all the numbers, just the largest number and smallest number you have seen so far. 

Think about how you could decide whether the number you just got from the user should be the new largest or smallest (hint: is it true that if it isn't the largest it is automatically the smallest?  Is it true that if it is the largest it can't also be the smallest?).

Also think about what values these variables should start as. Should the number that is going to be largest start high, or should it start low?

[EC] also find the second largest number. hint: there is more than one way a number could become the new second-largest.