LightbulbHelpTwo Dimensional Array

Posts 16 of 6 · Page 1 of 1
Two Dimensional Array
Write a Program in JAVA ,Two Dimensional Array
Example when you input
INPUT: 123456789101112
OUTPUT: 1 is Odd , 2 is Even , 3 is Odd ,4 is Even to so on
Use scanner to find the odds and evens.. Create an empty array, allow it so the user can input things in it, scan, check, sort.

int odd = s.nextInt(); blah blah.. Internet is full of tutorials..
Quote Originally Posted by banphacker View Post
Write a Program in JAVA ,Two Dimensional Array
Example when you input
INPUT: 123456789101112
OUTPUT: 1 is Odd , 2 is Even , 3 is Odd ,4 is Even to so on
The question is very vague. Not sure what you're asking. I've only been programming in Java for 2 weeks, but this is what I came up with. Obviously this isn't taking user input. My teacher still hasn't taught the class to do it, and I'm too lazy to look it up and incorporate it. Same goes for 2 dimensional arrays.
Code:
public class MPGH
{

    public static void main (String args[])
    {

        int control = 1;
        int[] myArray = new int[15];

        for (int index = 0; index < myArray.length; index++)
        {

            int r = (int)(Math.random() *10);
            myArray[index]= r;

            System.out.print(myArray[index] + " ");

        }

        for (int index = 0; index < myArray.length; index++)
        {

            if (control == 1)
            {

                myArray[index] = myArray[index] - myArray[index] * 2;
                control--;

            }

            else
            {

                control++;

            }

        }

        System.out.println("\n");

        for (int index = 0; index < myArray.length; index++)
        {

            System.out.print(myArray[index] + " ");

        }


    } //end main

} //end class Main
Actually. I did some trial and error and figured it out (the 2 dimensional array part and input)

Code:
import javax.swing.JOptionPane;

public class MPGH
{

    public static void main (String args[])
    {

        int control = 1;
        int[][] myArray = new int[15][2];

        for (int index = 0; index < 15; index++)
        {
            String numChoice = JOptionPane.showInputDialog( "Please enter a number!");
            int r = Integer.parseInt( numChoice );
            myArray[index][0]= r;

            System.out.print(myArray[index][0] + " "); //prints out [1 , 0], [2 , 0], ... [15, 0]

        }

        for (int index = 0; index < 15; index++)
        {

            if (control == 1)
            {

                myArray[index][1] = myArray[index][0] - myArray[index][0]* 2;
                control--;

            }

            else
            {

                myArray[index][1] = myArray[index][0];
                control++;

            }

        }

        System.out.println("\n");


        for (int index = 0; index < 15; index++)
        {

            System.out.print(myArray[index][1] + " "); //prints out [1 , 1], [2 , 1], ... [15, 1]

        }

    } //end main

} //end class Main
You don't need a 2D array for this.

Just parse the input, organize it into locations within an array, loop through it, and then use modulus to determine whether the contained number at each position is even or odd. Here's a small snippet of what you should be doing, calculation-wise.

Code:
for (int i = 0; i < array.length; i++) {
    if (array[i]%2 == 0) {
        System.out.print(array[i] + " is " + "even, ");
    } else {
        System.out.print(array[i] + " is " + "odd, ");
    }
}
Posts 16 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?