How to Go Back to Begining of Program in Java
#1
- New D.I.C Head
Reputation: 0
- Posts: 28
- Joined: 24-September 11
Loop back to the beginning of the program
Posted 27 October 2011 - 02:28 PM
I have a program almost completely finished, but I need the program to loop back to the beginning. Here is the code:
import java.util.Scanner; public class Tasks { public static void main(String[] args) { Scanner input = new Scanner(System.in); String entry; System.out.println("Please select one of the choices: "); System.out.println("Enter 1 for string functions."); System.out.println("Enter 2 for simple arithmetic functions."); System.out.println("Enter 3 for temperature conversion."); System.out.println("Enter 4 for sequences."); System.out.println("Enter 5 to exit."); entry = input.nextLine(); while (!entry.equals("1") && entry.equals("2") && entry.equals("3") && entry.equals("4") && entry.equals("5")) { System.out .println("Invalid entry. Please enter a number 1 through 5."); // Checks // to // make // sure // user // entry // is // valid entry = input.nextLine(); } if (entry.equals("1")) { stringFunctions(); } else if (entry.equals("2")) { arithmeticFunctions(); } else if (entry.equals("3")) { temperatureConversion(); } else if (entry.equals("4")) sequences(); else exit(); } /* * Method that asks for a phrase, and prints the first word, last word, and * length of the phrase. */ private static String stringFunctions() { Scanner input = new Scanner(System.in); String message, firstWord, lastWord; int pos, pos2, length; System.out.println("Enter a multiple word phrase: "); message = input.nextLine(); while (!message.contains(" ")) { System.out .println("Invalid entry. Please enter a multiple word phrase."); message = input.nextLine(); } pos = message.indexOf(" "); firstWord = message.substring(0, pos); System.out.println("The first word of the phrase is " + firstWord); pos2 = message.lastIndexOf(" "); lastWord = message.substring(pos2 + 1); System.out.println("The last word of the phrase is " + lastWord); length = message.length(); System.out.println("The length of the phrase is " + length); return message; } /* * Method that asks the user for two integer values then prints the sum of * the integers, positive difference between the integers, the product of * the integers, the number of times the first is evenly divisible by the * second, the remainder from that division, and the full decimal quotient * when the first number is divided by the second number */ private static double arithmeticFunctions() { Scanner input = new Scanner(System.in); int num1; int num2; System.out.println("Please enter an integer value: "); num1 = input.nextInt(); System.out.println("Please enter another integer value: "); num2 = input.nextInt(); while (num1 == 0 || num2 == 0) { System.out .println("Invalid entry. Please enter an integer value: "); num1 = input.nextInt(); System.out.println("Please enter another integer value: "); num2 = input.nextInt(); } System.out.println(num1 + " + " + num2 + " = " + (num1 + num2)); if (num1 > num2) { System.out.println(num1 + " - " + num2 + " = " + (num1 - num2)); } if (num2 > num1) { System.out.println(num1 + " - " + num2 + " = " + (-(num1 - num2))); } System.out.println(num1 + " * " + num2 + " = " + (num1 * num2)); System.out.println(num1 + " / " + num2 + " = " + (num1 / num2) + " remainder " + (num1 % num2)); System.out .println(num1 + " / " + num2 + " = " + ((double) num1 / num2)); return num1; } /* * Method that converts fahrenheit to celsius and celsius to fahrenheit. */ public static int temperatureConversion() { Scanner input = new Scanner(System.in); int entry1, entry2, fahrenheit, celsius; System.out.println("Please enter a fahrenheit temperature: "); entry1 = input.nextInt(); celsius = (entry1 - 32) * 5 / 9; System.out.println(entry1 + " degrees fahrenheit is " + celsius + " degrees celsius."); System.out.println("Please enter a celsius temperature: "); entry2 = input.nextInt(); fahrenheit = entry2 * 9 / 5 + 32; System.out.println(entry2 + " degrees celsius is " + fahrenheit + " degrees fahrenheit."); return fahrenheit; } /* * Method that prints an arithmetic sequence that the user picks. From * there, if the user enters the correct answer for the sequence, method * prints correct. Otherwise it prints incorrect and the correct answer. */ public static int sequences() { Scanner input = new Scanner(System.in); int entry, ans1, ans2, ans3, ans4, ans5; System.out.println("Please enter an integer number 1 through 5"); entry = input.nextInt(); if (entry == 1) { System.out .println("4, 10, 16, 22, 28. What is the next element of the sequence?"); ans1 = input.nextInt(); if (ans1 != 34) System.out .println("Incorrect answer. The correct answer is 34"); else System.out.println("Correct."); } else if (entry == 2) { System.out .println("10, 9, 8, 7. What is the next element of the sequence?"); ans2 = input.nextInt(); if (ans2 != 6) System.out .println("Incorrect answer. The correct answer is 6."); else System.out.println("Correct."); } else if (entry == 3) { System.out .println("4, 10, 17, 25, 34. What is the next element of the sequence?"); ans3 = input.nextInt(); if (ans3 != 44) System.out .println("Incorrect answer. The correct answer is 44."); else System.out.println("Correct."); } else if (entry == 4) { System.out .println("34, 30, 25, 19. What is the next element of the sequence?"); ans4 = input.nextInt(); if (ans4 != 12) System.out .println("Incorrect answer. The correct answer is 12."); else System.out.println("Correct."); } else { System.out .println("0, 1, 1, 2, 3. What is the next element of the sequence?"); ans5 = input.nextInt(); if (ans5 != 5) System.out .println("Incorrect answer. The correct answer is 5."); else System.out.println("Correct."); } return entry; } /* * Method that prompts the user goodbye. */ private static void exit() { System.out.println("Thank you for using my program! Goodbye!"); } }
I feel like there is some line of code that should make it go back to the main method but I'm not sure. Any help is much appreciated.
Is This A Good Question/Topic? 0
#2 tkess17
- New D.I.C Head
Reputation: 0
- Posts: 28
- Joined: 24-September 11
Re: Loop back to the beginning of the program
Posted 27 October 2011 - 02:37 PM
Ah I figured it out. I just had to add main(null); at the end of each method.
How to Go Back to Begining of Program in Java
Source: https://www.dreamincode.net/forums/topic/253171-loop-back-to-the-beginning-of-the-program/
0 Response to "How to Go Back to Begining of Program in Java"
Postar um comentário