# My first program. opinions on what i could do better

• June 20th, 2011, 11:00 AM
derekxec
My first program. opinions on what i could do better
This is my first program i have ever made in java and i am wonder what you all think of how its laid out and what i could do better to improve it or on the next program i make....It's a damage calculator for a game i play online :D thanks

Code :

```package DamageCalc;   import java.util.Scanner;   public class DmgCalc { public static void main (String args[]) {   int attack,element;   Scanner input = new Scanner(System.in);   System.out.println("Enter your Attack"); attack = input.nextInt();   System.out.println("What is your Element? (Use 1-4) 1-Fire 2-Water 3-Earth 4-Wind"); element = input.nextInt();   if(attack == 0) { System.out.println("Your damage is 0."); }else{   switch(element) { case 1: attack =(int)((attack * 1.4 + 0 - 2 * 0.98) * 1.6); System.out.println("Your Damage against your weak element is " + attack); break; case 2: attack =(int)((attack * 1.4 + 0 - 2 * 0.98) * 1.3); System.out.println("Your Damage against your weak element is " + attack); break; case 3: attack =(int)((attack * 1.4 + 0 - 2 * 0.98) * 1.4); System.out.println("Your Damage against your weak element is " + attack); break; case 4: element =(int)((attack * 1.4 + 0 - 2 * 0.98) * 1.3); System.out.println("Your Damage against your weak element is " + element); break;   default: System.out.println("Exiting...Please follow directions."); } } } }```
• June 20th, 2011, 11:10 AM
KevinWorkman
Re: My first program. opinions on what i could do better
That's a great example of learning how to program by writing little programs that are useful for something else you already enjoy. That's the way to go.

You asked for a critique, but honestly, if it works, and it fits in your head (so you understand it really well), then I wouldn't spend too much time worrying about how to improve it. Just keep on trucking, keep programming, and in a few months you'll look back and think to yourself "WHAT was I thinking?!". Not that there's anything blatantly wrong with the program, but that feeling happens to everybody.

That being said, are these just constant numbers, or do/will they change? You could just store them in an array at the corresponding index instead of using a switch statement. But like I said, don't worry too much about it.
• June 20th, 2011, 11:27 AM
derekxec
Re: My first program. opinions on what i could do better
this is exactly what i was looking for thanks for responding :D

the only numbers that change are the attack and the end number on the calculation(1.6 1.3 1.4) depending on the element chosen everything else stays the same

the next chapter in my book is on arrays so ill try to drop the switch statement for an array :D
• June 20th, 2011, 11:42 AM
william
Re: My first program. opinions on what i could do better
Quote:

Originally Posted by derekxec
This is my first program i have ever made in java

I think Kevin Said it all, great job on that switch statement, error checking on your input lines. Keep Programming challenges and increase functionality. To help keep track of your logic write it down on paper and then follow your design. Pseudo code helps think out your program logic before you have hours of wasted time.

Keep up the good work.