Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this thing has two version, the other one is at the bottom
- import java.util.Scanner;
- public class AkaruiKuraku {
- public static void main(String []args) {
- Scanner choice = new Scanner(System.in);
- int what;
- int number;
- do {
- System.out.println("\nSum of numbers from 1 to a user defined limit");
- System.out.println("\nChoose what method to use:");
- System.out.println("1 - For loop");
- System.out.println("2 - While loop");
- System.out.println("3 - Do loop");
- System.out.println("\n4 - Exit\n");
- System.out.print("Input: ");
- what = checkInputType(choice);
- choice.nextLine();
- if (what < 5 && what >= 1 && what != 4) {
- System.out.print("\nEnter number limit: ");
- number = checkInputType(choice);
- choice.nextLine();
- } else if (what == 4) {
- System.out.print("...");
- break;
- } else {
- System.out.println("\nInvalid, try again.\n");
- continue;
- }
- int result;
- switch (what) {
- case 1:
- result = limitCalcMethod1(number);
- System.out.println("\nResult: " + result);
- break;
- case 2:
- result = limitCalcMethod2(number);
- System.out.println("\nResult: " + result);
- break;
- case 3:
- result = limitCalcMethod3(number);
- System.out.println("\nResult: " + result);
- break;
- }
- } while (what != 4);
- choice.close();
- }
- static int limitCalcMethod1(int number) {
- int i;
- int con = 0;
- for (i = 1; i <= number; i++) {
- con += i;
- }
- return con;
- }
- static int limitCalcMethod2(int number) {
- int i = 1;
- int con = 0;
- while (i <= number) {
- con += i;
- i++;
- }
- return con;
- }
- static int limitCalcMethod3(int number) {
- int i = 1;
- int con = 0;
- do {
- con += i;
- i++;
- } while (i <= number);
- return con;
- }
- static int checkInputType(Scanner choice) {
- while (true) {
- if (choice.hasNextInt()) {
- return choice.nextInt();
- }
- else {
- System.out.println("Invalid type, please try again.");
- choice.next();
- }
- }
- }
- }
- //optimized ver. below
- import java.util.Scanner;
- public class AkaruiKuraku {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int choice;
- int number;
- while (true) {
- printMenu();
- choice = getValidInput(scanner);
- if (choice == 4) {
- System.out.println("Exiting...");
- break;
- }
- if (choice >= 1 && choice <= 3) {
- System.out.print("\nEnter number limit: ");
- number = getValidInput(scanner);
- int result;
- result = calculateSum(choice, number);
- System.out.println("\nResult: " + result);
- } else {
- System.out.println("\nInvalid, try again.\n");
- }
- }
- scanner.close();
- }
- static void printMenu() {
- System.out.println("\nSum of numbers from 1 to a user defined limit");
- System.out.println("\nChoose what method to use:");
- System.out.println("1 - For loop");
- System.out.println("2 - While loop");
- System.out.println("3 - Do loop");
- System.out.println("\n4 - Exit\n");
- System.out.print("Input: ");
- }
- static int calculateSum(int method, int number) {
- int sum = 0;
- switch (method) {
- case 1:
- for (int i = 1; i <= number; i++) {
- sum += i;
- }
- break;
- case 2:
- int i = 1;
- while (i <= number) {
- sum += i;
- i++;
- }
- break;
- case 3:
- i = 1;
- do {
- sum += i;
- i++;
- } while (i <= number);
- break;
- }
- return sum;
- }
- static int getValidInput(Scanner scanner) {
- while (!scanner.hasNextInt()) {
- System.out.println("Invalid type, please try again.");
- scanner.next();
- }
- return scanner.nextInt();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement