Jump to content

For loop maddness


Bow_In_Honor

Recommended Posts

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. :lol:

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 by Bow_In_Honor
  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.