Results 1 to 5 of 5
  1. #1
    aldorfski_17 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default Calculating Squares and Cubes in a table

    Hey All,

    I'm sorry if I sound rushed, but I need some help with one of my assignments. I am having some issues coding my "for" loops. I have an assignment that requests a table that receives an integer input. Once I input an integer, the table will display all of the squared and cubed numbers of every number leading up to that input. See example below:

    enter integer: 3

    Number Square Cube
    1 1 1
    2 4 8
    3 9 27

    continue? (y/n):

    The code I have just keeps repeating and does not increment. I tried incrementing up to the input, but that didn't work either. Any help is greatly appreciated. Thanks.

    -Aldorfski

    See code below:

    Java Code:
    import java.util.Scanner;
    
    public class PowerTableDisplayer {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		// Code to display "Welcome to the Squares and Cubes Table
    		System.out.println("Welcome to the Squares and Cubes Table");
    		System.out.println();
    		
    		// Create a Scanner object
    		Scanner sc = new Scanner(System.in);
    		
    		
    		
    		String choice = "y";
    		while(choice.equalsIgnoreCase("y"))
    		{
    		    	// get the input from the user
    				System.out.println("Enter an Integer: ");
                    int integerNext = sc.nextInt();     
                    
                   
                    System.out.println("Number" + "  " + "Squared" + "  " + "Cubed");      	
            		System.out.println("======" + "  " + "======" + "  " + "======");
            		
           
            for (int i = 1; i > 0; i++)
            {      
            i = integerNext;
            int numberSquared = (int) Math.pow(i, 2);
            int numberCubed = (int) Math.pow (i, 3);
            
            
            String message = "\n" + i + "       " + numberSquared + "       " + numberCubed;
            
            System.out.println(message);
            System.out.println();
            
            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();
            
            }
            
            
            
            }
    		
            
            
    		}
    			   
            			}
    Last edited by Eranga; 03-23-2010 at 03:37 AM. Reason: added code tags

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Well, when you start your for loop, you set the value of i to integerNext, so every iteration works with the same value. I think a much better way would be like this:
    Java Code:
    for(int i = 1; i <= integerNext; i++) {
    //the rest of the code goes here
    }
    And learn to use code tags and proper indentation, readability is important, especially if you want others to read your code.

  3. #3
    aldorfski_17 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default Getting there

    Hey MoonChile,

    Thanks for your reply. After implementing the change, the program ends after the first line.

    Enter integer: 3

    Number Squared Cubed
    3 9 27

    Any thoughts on how I can get the Number 1 and 2 to show up, as well as the Square and Cubed numbers for each?

    -Aldorfski

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Remove this line from the body of your loop:

    Java Code:
    i = integerNext;
    kind regards,

    Jos

  5. #5
    aldorfski_17 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default Thanks

    Hey All,

    Thanks for your help. This is great.

    -Aldorfski

Similar Threads

  1. need help with calculating something
    By mikec420 in forum New To Java
    Replies: 13
    Last Post: 09-29-2011, 10:14 PM
  2. Replies: 1
    Last Post: 01-08-2010, 07:19 AM
  3. help with perfect squares
    By AmplifiedKid in forum New To Java
    Replies: 1
    Last Post: 09-19-2009, 08:44 PM
  4. Perfect Squares
    By divyachaparala in forum New To Java
    Replies: 4
    Last Post: 02-05-2008, 10:21 AM
  5. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •