Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.Scanner;
- public class RunningLoops{
- private static Scanner inScanner = new Scanner (System.in);
- public static void main(String[] args) {
- printMenu();
- int menuSelection = inScanner.nextInt();
- while(menuSelection != 8){
- switch(menuSelection){
- case 1:
- fibonacci();
- break;
- case 2:
- plusOne();
- break;
- case 3:
- minusOne();
- break;
- case 4:
- guessNumber1();
- break;
- case 5:
- perfectNumber();
- break;
- case 6:
- remainder();
- break;
- case 7:
- repeat();
- break;
- case 8:
- menuSelection();
- break;
- default:
- System.out.println("Invalid selection.");
- }
- printMenu();
- menuSelection = inScanner.nextInt();
- }
- }
- private static void menuSelection() {
- // TODO Auto-generated method stub
- }
- public static void printMenu(){
- System.out.println("| MAIN MENU |");
- System.out.println("1.)Fibonacci");
- System.out.println("2.)Add One");
- System.out.println("3.)Subtract One");
- System.out.println("4.)Guess Number");
- System.out.println("5.)Perfect Number Checker");
- System.out.println("6.)Remainder");
- System.out.println("7.)Repeat");
- System.out.println("8.)Exit");
- System.out.println("Select the number of the program you would like to run: ");
- }
- public static void fibonacci() {
- System.out.println("Enter the number you want in fibonacci");
- Scanner input1 = new Scanner (System.in);
- int n = input1.nextInt();
- if (n == 0) {
- System.out.println("0");
- } else if (n == 1) {
- System.out.println("0 1");
- } else {
- System.out.print("0 1 ");
- int a = 0;
- int b = 1;
- for (int i = 1; i < n; i++) {
- int nextNumber = a + b;
- System.out.print(nextNumber + " ");
- a = b;
- b = nextNumber;
- }
- }
- }
- public static void plusOne(){
- System.out.println("Gimme a number to add one too. ");
- Scanner input2 = new Scanner (System.in);
- int n = input2.nextInt();
- for(int x =0; x<=n; x++ ){
- System.out.println(x);
- ///return x;
- }
- }
- public static void minusOne(){
- System.out.println("Gimme a number to add one too. ");
- Scanner input2 = new Scanner (System.in);
- int n = input2.nextInt();
- for(int x =n; x>=0; x-- ){
- System.out.println(x);}
- ///return x;
- }
- public static void guessNumber1() {
- String[] args = null;
- GuessNumber.main(args);
- }
- public static void perfectNumber(){
- System.out.print("Input positive integer: ");
- int n = inScanner.nextInt();
- while (n < 0){
- System.out.println("Invalid section.");
- System.out.print("Input positive integer: ");
- n = inScanner.nextInt();
- }
- System.out.println("Is " + n +" a perfect nuumber?: " + (isPerfectNumber(n) ? "Yes" : "No"));
- }
- public static boolean isPerfectNumber(int n){
- if (n < 2)
- return false;
- int sum = 0;
- for (int d = 2; d < Math.sqrt(n); d++){
- if (n % d == 0){
- sum += d;
- }
- }
- return sum == n;
- }
- public static void repeat(){
- System.out.println("What would you like to repeat? ");
- Scanner input2 = new Scanner (System.in);
- String n = input2.nextLine();
- System.out.println("How many times would you like to repeat it? ");
- Scanner input3 = new Scanner (System.in);
- int x = input3.nextInt();
- for(int i=0; i<=x; i++){
- System.out.println(n);
- }
- }
- public static void menuSelection1(){
- System.out.println("Trollarch ");
- }
- public static void exit(){
- }
- public static void remainder(){
- System.out.println("Please enter a number ");
- Scanner input = new Scanner (System.in);
- int x = input.nextInt();
- System.out.println("Please enter a number you want to divide the first with ");
- Scanner input1 = new Scanner (System.in);
- int d = input1.nextInt();
- int q = x % d;
- System.out.println("Remainder Is: "+ q);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement