Java help needed.. badly.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • derbys
    New Member
    • Oct 2006
    • 6

    #1

    Java help needed.. badly.

    Hey im stuck on a question that i need to do, i have pretty much the basics of the programme but am not sure how to get it implemented so that it'll work. The question is :

    Declare a two dimensional array with 10 rows and 10 columns and initialise:
    a) All the elements of its third row to 10
    b) All the elements of its 6th column to 20
    c) And all the other elements to 5
    in that order.

    I have the following piece of code that is pretty much the core of the program:

    Code:
    import uwejava.*;
    import glos.*;
    
    public class exc4 
    {
      
     public int nums;
      
    
     for(int y = 0; y < nums.length; ++y)
      for(int x = 0; x < nums[i].length; ++x)
        if(y == 2)
          nums[y][x] = 10;
        else if(x == 19)
          nums[y][x] = 20;
        else
          nums[y][x] = 5;
      }
    So if anyone can figure out how to implemet it to work correctly id be delighted. Two solid days of java has pretty much turned my brain to mush :(
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by derbys
    Hey im stuck on a question that i need to do, i have pretty much the basics of the programme but am not sure how to get it implemented so that it'll work. The question is :

    Declare a two dimensional array with 10 rows and 10 columns and initialise:
    a) All the elements of its third row to 10
    b) All the elements of its 6th column to 20
    c) And all the other elements to 5
    in that order.

    I have the following piece of code that is pretty much the core of the program:

    Code:
    import uwejava.*;
    import glos.*;
    
    public class exc4 
    {
      
     public int nums;
      
    
     for(int y = 0; y < nums.length; ++y)
      for(int x = 0; x < nums[i].length; ++x)
        if(y == 2)
          nums[y][x] = 10;
        else if(x == 19)
          nums[y][x] = 20;
        else
          nums[y][x] = 5;
      }
    So if anyone can figure out how to implemet it to work correctly id be delighted. Two solid days of java has pretty much turned my brain to mush :(
    Code:
    public class Exc4 {
    	public static void main(String[] args) { // every java application has to have this method
    		int[][] nums = new int[10][10];//declares a 10 x 10 array of ints
    		for(int i = 0; i < nums.length; i++) {
    			for(int j = 0; j < nums.length; j++) {
    				if(i == 2) {
    					nums[i][j] = 10;
    				}
    				else if(j == 5) {
    					nums[i][j] = 20;
    				}
    				else {
    					nums[i][j] = 5;
    				}
    			}
    		}
    		for(int i = 0; i < nums.length; i++) {
    			for(int j = 0; j < nums.length; j++) {
    				System.out.print(""+nums[i][j]);
    			}
    			System.out.println();
    		}
    	}
    }

    Comment

    • derbys
      New Member
      • Oct 2006
      • 6

      #3
      That's brillaint thanks, and opne more question (im a cheeky so and so, i know) I need to

      -Create a 50-element integer array
      -Fills this array with the random numbers in the range 1 to 10000 and display it.

      I *think* i know how i create the integer and random numbers, with the code below - but im not 100% sure on how i should go about displaying it, any help again would be brilliant.

      Code:
      int[] myArray= new int[50]; // Declaring 50 integers
      
      for (int i = 0; i < myArray.length; i++) { //Random number
      			Double random = new Double(Math.random() * 10000);
      			int randomInt = random.intValue();
      			myArray[i] = randomInt;
      
      		}
      I think that's what it is, just need to know what to do to display it

      Cheers

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by derbys
        That's brillaint thanks, and opne more question (im a cheeky so and so, i know) I need to

        -Create a 50-element integer array
        -Fills this array with the random numbers in the range 1 to 10000 and display it.

        I *think* i know how i create the integer and random numbers, with the code below - but im not 100% sure on how i should go about displaying it, any help again would be brilliant.

        Code:
        int[] myArray= new int[50]; // Declaring 50 integers
        
        for (int i = 0; i < myArray.length; i++) { //Random number
        			Double random = new Double(Math.random() * 10000);
        			int randomInt = random.intValue();
        			myArray[i] = randomInt;
        
        		}
        I think that's what it is, just need to know what to do to display it

        Cheers
        Code:
        for(int i = 0; i < myArray.length; i++) {
           System.out.print(myArray[i] + " ");// will print all numbers in one line
        }
        or if you are using jdk 1.5

        Code:
        for(int i : myArray) {
           System.out.print(i + " ");
        }

        Comment

        Working...