Bow_In_Honor Posted May 13, 2015 Share Posted May 13, 2015 (edited) One of the assignments for my final was to write an application that asked the user to input 10 integers, and then separate them into 2 arrays, one to hold numbers below 50, and one for numbers higher than 50, then display both arrays. I had a little fun with the for loop. Don't make fun of my shitty code. import java.util.Scanner; public class ListsOfIntegers { public static void main(String[] args) { Scanner input = new Scanner(System.in); int arrayOfInt[] = new int[10]; final int NUMBEROFINT = 10; System.out.println("Enter an integer"); for(int x = 0; x < NUMBEROFINT; x++) { arrayOfInt[x] = input.nextInt(); System.out.println("Enter another integer"); } int counter = 0; for(int y = 0; y < NUMBEROFINT; y++) { if (arrayOfInt[y] > 50) { counter++; } } int secArray[] = new int[counter]; int arrayCounter = 0; for(int j = 0; j < NUMBEROFINT; j++) { if (arrayOfInt[j] > 50) { secArray[arrayCounter] = arrayOfInt[j]; arrayCounter++; } } int counter2 = 0; for(int i = 0; i < NUMBEROFINT; i++) { if (arrayOfInt[i] < 50) { counter2++; } } int thirdArray[] = new int[counter2]; arrayCounter = 0; for(int k = 0; k < NUMBEROFINT; k++) { if (arrayOfInt[k] < 50) { thirdArray[arrayCounter] = arrayOfInt[k]; arrayCounter++; } } System.out.println("All numbers above 50"); for(int b = 0; b < counter; b++) { System.out.print(secArray[b] + " "); } System.out.println("\n\nAll numbers below 50"); for(int c = 0; c < counter2; c++) { System.out.print(thirdArray[c] + " "); } System.out.println("=F|A= FTW"); } } Edited May 13, 2015 by Bow_In_Honor 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.