Advertisement
Guest User

ASDF

a guest
Nov 24th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.Scanner;
  3. public class RunningLoops{
  4.  
  5. private static Scanner inScanner = new Scanner (System.in);
  6. public static void main(String[] args) {
  7.  
  8. printMenu();
  9. int menuSelection = inScanner.nextInt();
  10.  
  11. while(menuSelection != 8){
  12. switch(menuSelection){
  13. case 1:
  14. fibonacci();
  15. break;
  16. case 2:
  17. plusOne();
  18. break;
  19. case 3:
  20. minusOne();
  21. break;
  22. case 4:
  23. guessNumber1();
  24. break;
  25. case 5:
  26. perfectNumber();
  27. break;
  28. case 6:
  29. remainder();
  30. break;
  31.  
  32. case 7:
  33. repeat();
  34. break;
  35. case 8:
  36. menuSelection();
  37. break;
  38.  
  39. default:
  40. System.out.println("Invalid selection.");
  41. }
  42.  
  43. printMenu();
  44. menuSelection = inScanner.nextInt();
  45. }
  46.  
  47.  
  48. }
  49.  
  50. private static void menuSelection() {
  51. // TODO Auto-generated method stub
  52.  
  53. }
  54.  
  55.  
  56. public static void printMenu(){
  57. System.out.println("| MAIN MENU |");
  58. System.out.println("1.)Fibonacci");
  59. System.out.println("2.)Add One");
  60. System.out.println("3.)Subtract One");
  61. System.out.println("4.)Guess Number");
  62. System.out.println("5.)Perfect Number Checker");
  63. System.out.println("6.)Remainder");
  64. System.out.println("7.)Repeat");
  65. System.out.println("8.)Exit");
  66. System.out.println("Select the number of the program you would like to run: ");
  67. }
  68.  
  69. public static void fibonacci() {
  70. System.out.println("Enter the number you want in fibonacci");
  71. Scanner input1 = new Scanner (System.in);
  72. int n = input1.nextInt();
  73. if (n == 0) {
  74. System.out.println("0");
  75. } else if (n == 1) {
  76. System.out.println("0 1");
  77. } else {
  78. System.out.print("0 1 ");
  79. int a = 0;
  80. int b = 1;
  81. for (int i = 1; i < n; i++) {
  82. int nextNumber = a + b;
  83. System.out.print(nextNumber + " ");
  84. a = b;
  85. b = nextNumber;
  86. }
  87. }
  88. }
  89. public static void plusOne(){
  90. System.out.println("Gimme a number to add one too. ");
  91. Scanner input2 = new Scanner (System.in);
  92. int n = input2.nextInt();
  93.  
  94. for(int x =0; x<=n; x++ ){
  95. System.out.println(x);
  96. ///return x;
  97. }
  98.  
  99. }
  100. public static void minusOne(){
  101. System.out.println("Gimme a number to add one too. ");
  102. Scanner input2 = new Scanner (System.in);
  103. int n = input2.nextInt();
  104.  
  105. for(int x =n; x>=0; x-- ){
  106. System.out.println(x);}
  107. ///return x;
  108. }
  109.  
  110. public static void guessNumber1() {
  111.  
  112. String[] args = null;
  113. GuessNumber.main(args);
  114. }
  115.  
  116. public static void perfectNumber(){
  117. System.out.print("Input positive integer: ");
  118. int n = inScanner.nextInt();
  119.  
  120. while (n < 0){
  121. System.out.println("Invalid section.");
  122. System.out.print("Input positive integer: ");
  123. n = inScanner.nextInt();
  124. }
  125.  
  126. System.out.println("Is " + n +" a perfect nuumber?: " + (isPerfectNumber(n) ? "Yes" : "No"));
  127. }
  128.  
  129. public static boolean isPerfectNumber(int n){
  130. if (n < 2)
  131. return false;
  132.  
  133. int sum = 0;
  134. for (int d = 2; d < Math.sqrt(n); d++){
  135. if (n % d == 0){
  136. sum += d;
  137. }
  138. }
  139.  
  140. return sum == n;
  141. }
  142.  
  143. public static void repeat(){
  144. System.out.println("What would you like to repeat? ");
  145. Scanner input2 = new Scanner (System.in);
  146. String n = input2.nextLine();
  147. System.out.println("How many times would you like to repeat it? ");
  148. Scanner input3 = new Scanner (System.in);
  149. int x = input3.nextInt();
  150.  
  151. for(int i=0; i<=x; i++){
  152. System.out.println(n);
  153. }
  154.  
  155. }
  156.  
  157. public static void menuSelection1(){
  158. System.out.println("Trollarch ");
  159. }
  160.  
  161.  
  162. public static void exit(){
  163.  
  164. }
  165. public static void remainder(){
  166. System.out.println("Please enter a number ");
  167. Scanner input = new Scanner (System.in);
  168. int x = input.nextInt();
  169. System.out.println("Please enter a number you want to divide the first with ");
  170. Scanner input1 = new Scanner (System.in);
  171. int d = input1.nextInt();
  172.  
  173. int q = x % d;
  174. System.out.println("Remainder Is: "+ q);
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement