/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code14feb19; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code14Feb19 { /** * @param args the command line arguments */ public static void main(String[] args) { // Loop within a loop to print a table int current = -1; int target = 50; int index = -1; int numTgt = 0; System.out.println("Generating random numbers to search. Making table with 10 columns by % 10"); for (int i = 0; i < 100; i++) // (initialization; test; increment) { current = (int) (Math.random() * 100); //System.out.println(i+ ". "+current); if ((i % 10) == 0) System.out.println(""); System.out.print(current+"\t"); if (target == current) { index = i; numTgt++; } } System.out.println(""); if (index > -1) { System.out.println("Found target of "+target+" at index "+index+" with count of "+numTgt); } else { System.out.println("Target "+target+"was not in data set"); } System.out.println("\n"); // i = 4; i is not in the scope at this point System.out.println("\nPrinting table with nested for loops"); int k; current = 0; for (k = 0; k < 5; k++) { for (int j = 0; j < 5; j++) { System.out.print(current + "\t"); current += 2; } System.out.println(""); } System.out.println(""); int j = 0; current = 100; System.out.println("\nEmpty for loop header example"); for (; j < 5; ) // notice that for loop can have empty initialization and increment sections { System.out.print(current + "\t"); current -= 2; j++; } System.out.println("\n"); // Don't do this in Dr. T's class // Do not mix the loop control with the loop body elements in the for loop header System.out.println("\nUgly for loop header example"); for (int m = 0, c = 300; m < 5; m++, c -= 2) { System.out.print(c + "\t"); } // Extra loop examples to print a table System.out.println("\n\nExtra loop examples to print a table\n"); int value = 1; final int MAX = 6; int j2 = 0; // while loop inside a for loop System.out.println("\nPrinting 2nd matrix/table by using while loop inside a for loop"); for (int k2 = 0; k2 < MAX; k2++) { j2 = 0; while (j2 < MAX) { System.out.print((value++)+"\t"); j2++; } System.out.println(""); } System.out.println("\nPrinting 3rd matrix/table by using for loop inside a while loop"); value = 0; j = 0; //for (int k = 0; k < MAX*2; k++) while (j < MAX*2) { for (int k3 = 0; k3 < j; k3++) { System.out.print((value)+"\t"); } System.out.println(""); value++; j++; } System.out.println("\nComparing current and previous values\n"); // Compare current input to previous input 5 times and int curr = -1; int prev = -1; Scanner input = new Scanner(System.in); System.out.println("Please enter a positive integer less than 100: "); prev = input.nextInt(); prev = errorCheckPos50(prev); /* while ((prev >= 50) || (prev < 0)) { System.out.println("Your input was invalid. Please enter another positive integer less than 50: "); prev = input.nextInt(); } */ for (int n = 0; n < 5; n++) { System.out.println("Please enter another positive integer less than 100: "); curr = input.nextInt(); curr = errorCheckPos50(curr); // loop to validate input (ex. valid range of values) /* while ((curr >= 50) || (curr < 0)) { System.out.println("Your input was invalid. Please enter another positive integer less than 50: "); curr = input.nextInt(); } */ if (curr > prev) { System.out.println("Your current input "+curr+" is larger than your last input of "+prev); } else if (curr < prev) { System.out.println("Your current input "+curr+" is smaller than your last input of "+prev); } else { System.out.println("Your current input "+curr+" is equal to your last input of "+prev); } prev = curr; } } public static int errorCheckPos50(int value) { Scanner input = new Scanner(System.in); while ((value >= 100) || (value < 0)) { System.out.println("Your input was invalid. Please enter another positive integer less than 50: "); value = input.nextInt(); } return value; } }