Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Programming tips

Posted on 2009-07-06
1
Medium Priority
?
317 Views
Last Modified: 2013-11-23
There once was a wise servant who saved the life of a princess. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.

There are 64 squares on a chessboard.

Write a program that shows how many grains were on each square and the total number of grains.

In other words, I want to type

    java Grains

and see

    square 1:  1 grain
    square 2:  2 grains
    square 3:  4 grains
    square 4:  8 grains

etc.

public class Chess {
      public static void main(String[] args) {
            try{
                  int enteredNumber = Integer.parseInt(args[0]);
                  if( enteredNumber < 1 || enteredNumber > 64){
                        System.out.print("invalid numbers");
                        System.exit(0);
                  }
                  int numberOfSquares = (int)Math.pow(2, (enteredNumber - 1));
                  System.out.println(numberOfSquares);
            }catch(NumberFormatException e){
                  e.printStackTrace();
            }
      }
}
0
Comment
Question by:SunScreenCert
1 Comment
 
LVL 20

Accepted Solution

by:
a_b earned 2000 total points
ID: 24791766
Run a for loop from 0 to 63 that does the calculation for you
for(int i=0;i<64;i++)
{  
       numberOfSquares = (int) Math.pow(2,i);
       sysout(numberOfSquares);
}

Right now the program is only running for a single number you need to change the dependency on the args[0].
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question