Advertisement
AkaruiKuraku

Sum of numbers from 1 to user-defined limit

May 13th, 2025 (edited)
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | Source Code | 0 0
  1. //this thing has two version, the other one is at the bottom
  2.  
  3. import java.util.Scanner;
  4. public class AkaruiKuraku {
  5.    
  6.     public static void main(String []args) {
  7.         Scanner choice = new Scanner(System.in);
  8.         int what;
  9.         int number;
  10.        
  11.         do {
  12.             System.out.println("\nSum of numbers from 1 to a user defined limit");
  13.             System.out.println("\nChoose what method to use:");
  14.             System.out.println("1 - For loop");
  15.             System.out.println("2 - While loop");
  16.             System.out.println("3 - Do loop");
  17.             System.out.println("\n4 - Exit\n");
  18.             System.out.print("Input: ");
  19.             what = checkInputType(choice);
  20.             choice.nextLine();
  21.            
  22.             if (what < 5 && what >= 1 && what != 4) {
  23.             System.out.print("\nEnter number limit: ");
  24.             number = checkInputType(choice);
  25.             choice.nextLine();
  26.             } else if (what == 4) {
  27.                 System.out.print("...");
  28.                 break;
  29.             } else {
  30.                 System.out.println("\nInvalid, try again.\n");
  31.                 continue;
  32.             }
  33.            
  34.             int result;
  35.             switch (what) {
  36.                 case 1:
  37.                     result = limitCalcMethod1(number);
  38.                     System.out.println("\nResult: " + result);
  39.                     break;
  40.                 case 2:
  41.                     result = limitCalcMethod2(number);
  42.                     System.out.println("\nResult: " + result);
  43.                     break;
  44.                 case 3:
  45.                     result = limitCalcMethod3(number);
  46.                     System.out.println("\nResult: " + result);
  47.                     break;
  48.             }
  49.         } while (what != 4);
  50.        
  51.         choice.close();
  52.     }
  53.     static int limitCalcMethod1(int number) {
  54.         int i;
  55.         int con = 0;
  56.         for (i = 1; i <= number; i++) {
  57.             con += i;
  58.         }
  59.         return con;
  60.     }
  61.     static int limitCalcMethod2(int number) {
  62.         int i = 1;
  63.         int con = 0;
  64.         while (i <= number) {
  65.             con += i;
  66.             i++;
  67.         }
  68.         return con;
  69.     }
  70.     static int limitCalcMethod3(int number) {
  71.         int i = 1;
  72.         int con = 0;
  73.         do {
  74.             con += i;
  75.             i++;
  76.         } while (i <= number);
  77.         return con;
  78.     }
  79.     static int checkInputType(Scanner choice) {
  80.         while (true) {
  81.             if (choice.hasNextInt()) {
  82.                 return choice.nextInt();
  83.             }
  84.             else {
  85.                 System.out.println("Invalid type, please try again.");
  86.                 choice.next();
  87.             }
  88.         }
  89.     }
  90. }
  91.  
  92. //optimized ver. below
  93.  
  94. import java.util.Scanner;
  95.  
  96. public class AkaruiKuraku {
  97.     public static void main(String[] args) {
  98.         Scanner scanner = new Scanner(System.in);
  99.         int choice;
  100.         int number;
  101.  
  102.         while (true) {
  103.             printMenu();
  104.             choice = getValidInput(scanner);
  105.  
  106.             if (choice == 4) {
  107.                 System.out.println("Exiting...");
  108.                 break;
  109.             }
  110.  
  111.             if (choice >= 1 && choice <= 3) {
  112.                 System.out.print("\nEnter number limit: ");
  113.                 number = getValidInput(scanner);
  114.  
  115.                 int result;
  116.                 result = calculateSum(choice, number);
  117.                 System.out.println("\nResult: " + result);
  118.             } else {
  119.                 System.out.println("\nInvalid, try again.\n");
  120.             }
  121.         }
  122.  
  123.         scanner.close();
  124.     }
  125.  
  126.     static void printMenu() {
  127.         System.out.println("\nSum of numbers from 1 to a user defined limit");
  128.         System.out.println("\nChoose what method to use:");
  129.         System.out.println("1 - For loop");
  130.         System.out.println("2 - While loop");
  131.         System.out.println("3 - Do loop");
  132.         System.out.println("\n4 - Exit\n");
  133.         System.out.print("Input: ");
  134.     }
  135.  
  136.     static int calculateSum(int method, int number) {
  137.         int sum = 0;
  138.         switch (method) {
  139.             case 1:
  140.                 for (int i = 1; i <= number; i++) {
  141.                     sum += i;
  142.                 }
  143.                 break;
  144.             case 2:
  145.                 int i = 1;
  146.                 while (i <= number) {
  147.                     sum += i;
  148.                     i++;
  149.                 }
  150.                 break;
  151.             case 3:
  152.                 i = 1;
  153.                 do {
  154.                     sum += i;
  155.                     i++;
  156.                 } while (i <= number);
  157.                 break;
  158.         }
  159.         return sum;
  160.     }
  161.  
  162.     static int getValidInput(Scanner scanner) {
  163.         while (!scanner.hasNextInt()) {
  164.             System.out.println("Invalid type, please try again.");
  165.             scanner.next();
  166.         }
  167.         return scanner.nextInt();
  168.     }
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement