Results 1 to 5 of 5
- 03-22-2010, 06:07 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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 02:37 AM. Reason: added code tags
- 03-22-2010, 06:17 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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:
And learn to use code tags and proper indentation, readability is important, especially if you want others to read your code.Java Code:for(int i = 1; i <= integerNext; i++) { //the rest of the code goes here }
- 03-22-2010, 06:40 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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
- 03-22-2010, 07:43 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Remove this line from the body of your loop:
kind regards,Java Code:i = integerNext;
Jos
- 03-22-2010, 07:17 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
need help with calculating something
By mikec420 in forum New To JavaReplies: 13Last Post: 09-29-2011, 09:14 PM -
How to repaint.refresh the table (table model) with combo box selection envent
By man4ish in forum AWT / SwingReplies: 1Last Post: 01-08-2010, 06:19 AM -
help with perfect squares
By AmplifiedKid in forum New To JavaReplies: 1Last Post: 09-19-2009, 07:44 PM -
Perfect Squares
By divyachaparala in forum New To JavaReplies: 4Last Post: 02-05-2008, 09:21 AM -
Calculating sin of a double value
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 08:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks