So I am creating simple program for my Java class and one of the last requirements is to have the program loop. I've done this before, but it was horribly inefficient and convoluted.
Basically, I used a switch case named start with values 0 and 1. If the user typed 0 a do-while loop would begin and if they typed 1, the program would terminate. The only problem with this is the user would have to type 0 for the program to even begin, and if they typed 1, I had to have a fail-safe changing the value of start to 3 or else an infinite loop would begin. Can someone help me find a better way of doing this? (Also, before anyone says anything about the way it is written, one of the requirements was that it HAD to be done within one executable class.) (Also, Also: Could someone tell me the rules of indentation? I'm really horrible at it.)
Here is my code below:
/*
* Simple Java Survey Program
*/
import java.util.Scanner;
public class J2Strings {
public static void main(String[] args) {
// TODO Auto-generated method stub
// variables
String fName;
String lName;
String mName;
int age;
String say;
String fFood;
String fTV;
Scanner userInput = new Scanner(System.in);
System.out.println("Today you are going to take a small personal survey");
System.out.println("");
System.out.println("Begin by entering your first name: ");
fName = userInput.next();
System.out.println("Enter your last name:");
lName = userInput.next();
System.out.println("Enter your middle name:");
mName = userInput.next();
System.out.println("Enter your age:");
age = userInput.nextInt();
System.out.println("Enter your favorite saying:");
say = userInput.next();
System.out.println("Enter your favorite food:");
fFood = userInput.next();
System.out.println("Finally, enter your favorite TV show:");
fTV = userInput.next();
char f = fName.charAt(0);
char l = lName.charAt(0);
char m = mName.charAt(0);
System.out.println("Based on the information you entered, here are your initials: " + f + "." + m + "." + l);
System.out.println("This is how old you will be in 50 years: " + (age + 50));
}
}