Help With Programming in Java (Using JEdit)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • djtosh
    Banned
    New Member
    • Mar 2007
    • 18

    #16
    Code:
     { 
    	static Scanner data_input = new Scanner(System.in);
     
    	// Method to calculate the area of a square
    	static void square(String [] args)
    	{
    	 int l, n, i, t, result = 0;
    		boolean quit;
     
    		// Get the length
    		System.out.print("Enter the length of a side of the square: ");
    		l = data_input.nextInt();
     
    				while(l <=0)
    	{
     
    		// Output error message
    		System.out.println("Length must be greater than 0");
     
    		// Get length
    		System.out.print("Enter the length: ");
    		l = data_input.nextInt();
    	}
    	 // Get the number of squares
    		System.out.print("Enter the number of squares you wish to calculate: ");
    		n = data_input.nextInt();
     
    			if (n <=0)
    		{
    		System.out.println("That number was not valid:");
    		quit = true;
     
    		// Get the number of squares
    		System.out.print("Enter the number of squares you wish to calculate: ");
    		n = data_input.nextInt();
    		}
     
    		// Get the Increment
    		System.out.print("Enter the size increment: ");
    		i = data_input.nextInt();
     
    			if (i <=0)
    		{
    		System.out.println("That number was not valid:");
    		quit = true;
     
    		// Get the Increment
    		System.out.print("Enter the size increment: ");
    		i = data_input.nextInt();
    		}
     
    		// Add the numbers
     
    		for(t = 0 ; t < n; t++ )
    		{
    result = (l * l);
    		// Display the result
    		System.out.print(l + " * ");
    System.out.print(l + " = ");
    System.out.println(result);
    		l = l + i;
    }
     
     
     
    	}

    Comment

    • djtosh
      Banned
      New Member
      • Mar 2007
      • 18

      #17
      this is how my code looked in the end and it worked perfectly, i used the array/loop that you gave me, it just needed slight rearrangement, thanks

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        Originally posted by djtosh
        this is how my code looked in the end and it worked perfectly, i used the array/loop that you gave me, it just needed slight rearrangement, thanks
        Good to hear that .

        Comment

        Working...