Goal: Create a basic loop that prints your name 10 times.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Refresher /// File Name: Refresher.java /// Date Finished: Feb 29, 2016 import java.util.Scanner; public class Refresher { public static void main(String[] args) { Scanner input = new Scanner(System.in); String name; int iterations; System.out.println("What is your name?"); System.out.print("> "); name = input.nextLine(); if (name.equalsIgnoreCase("josh")) { iterations = 5; } else { iterations = 10; } for (int i = 0; i < iterations; i++) { System.out.println(name); } } }
Goal: Complete the previous assignment, but display the name only 5 times if the input is "josh", and do it with only one if statement and one for loop.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Refresher Challenge /// File Name: RefresherChallenge.java /// Date Finished: Feb 29, 2016 import java.util.Scanner; public class RefresherChallenge { public static void main(String[] args) { Scanner input = new Scanner(System.in); String name; int iterations = 10; System.out.println("What is your name?"); System.out.print("> "); name = input.nextLine(); if (name.equalsIgnoreCase("josh")) { iterations = 5; } for (int i = 0; i < iterations; i++) { System.out.println(name); } } }
Goal: Display 12 multiples of a specified number.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Displaying Some Multiples /// File Name: DisplayingMultiples.java /// Date Finished: Feb 29, 2016 import java.util.Scanner; public class DisplayingMultiples { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num; System.out.println("Choose a number."); System.out.print("> "); num = input.nextInt(); for (int i = 1; i <= 12; i++) { System.out.println(num + " * " + i + " = " + (num * i)); } } }