Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. //Jordan Rust, Assignment 3, 11:00 AM, EXTRA CREDIT ATTEMPTED
  2.  
  3. import java.util.*;
  4.  
  5. public class IntFun
  6. {
  7.     public static void main (String[] args)
  8.     {
  9.         Scanner kb = new Scanner(System.in);
  10.        
  11.         //List of all variables.
  12.         int startNum = 0;
  13.         int userNum = 0;
  14.         int odds = 0;
  15.         int evens = 0;
  16.         int zeroes = 0;
  17.         int digit = 0;
  18.         int sum = 0;
  19.         int primeStart;
  20.        
  21.         //Start of the menu, just to be polite.
  22.         System.out.println("Hello there. Welcome to the number menu.");
  23.  
  24.         //Gets the user's first integer.
  25.         System.out.println("Please enter a positive integer: ");
  26.         startNum = kb.nextInt();
  27.        
  28.         //Makes sure the integer is positive.
  29.         if(startNum < 0)
  30.         {
  31.             System.out.println("That isn't a positive integer, please try again.");
  32.             startNum = kb.nextInt();
  33.         }
  34.        
  35.         //Prints the menu and offers the choice selection.
  36.         if(startNum > 0)
  37.         {
  38.             System.out.println("1.Enter a new number.\n2.Print the number of odd digits, even digits and zereos in the integer.\n3.Print the prime numbers between 2 and the integer.\n4.Print the sum of the digits in the integer.\n5.Quit the program.");
  39.             System.out.println("Your choice please: ");
  40.             userNum = kb.nextInt();
  41.         }
  42.        
  43.         //Fulfills option one of the menu.
  44.         if(userNum == 1)
  45.         {
  46.             System.out.println("Your new positive integer please: ");
  47.             startNum = kb.nextInt();
  48.         }
  49.        
  50.         //Fulfills option two of the menu.
  51.         if(userNum == 2)
  52.         {
  53.             boolean isSignificant = false;
  54.             int divisor = 100000000;
  55.             while(divisor > 0)
  56.             {
  57.                 digit = startNum/divisor;
  58.                 if(digit == 0 && isSignificant == true)
  59.                     zeroes ++;
  60.                 else if(digit%2 == 0)
  61.                     evens ++;
  62.                     isSignificant = true;
  63.                 if(digit%2 != 0)
  64.                     odds ++;
  65.                     isSignificant = true;
  66.                 startNum = startNum % divisor;
  67.                 divisor = divisor/10;
  68.             }
  69.            
  70.             System.out.println("Evens: " + evens + " Odds: " + odds + " Zeroes: " + zeroes);           
  71.         }
  72.        
  73.         //Fulfills option three of the menu.   
  74.         if(userNum == 3)
  75.         {
  76.             System.out.println("The prime numbers are: ");
  77.            
  78.             for (primeStart = 2; primeStart < startNum; primeStart++ )
  79.             {
  80.                 int primeSearch;
  81.                 for (primeSearch = 2; primeSearch < primeStart; primeSearch++)
  82.                 {
  83.                     int primes = primeStart%primeSearch;
  84.                     if (primes == 0)
  85.                     {
  86.                         break;
  87.                     }
  88.                 }
  89.                 if(primeStart == primeSearch)
  90.                 {
  91.                     System.out.print(" " + primeStart);
  92.                 }
  93.             }
  94.         }  
  95.        
  96.         //Fulfills option four of the menu.
  97.         if(userNum == 4)
  98.         {
  99.             int num = startNum;
  100.             while(num > 0)
  101.             {
  102.                 digit = num%10;
  103.                 sum += digit;
  104.                 num = num/10;
  105.             }
  106.            
  107.             System.out.println("The sum is: " + sum);
  108.         }
  109.        
  110.         //Fulfills option five of the menu.
  111.         if(userNum == 5)
  112.         {
  113.             System.out.println("You've chosen to quit. Have a nice day!");
  114.         }  
  115.        
  116.     }  
  117.        
  118. }
Add Comment
Please, Sign In to add comment