Solved

Programming tips

Posted on 2009-07-06
1
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 20

Accepted Solution

by:
a_b earned 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

628 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