There are excellent suggestions in the above text, and I'd echo most of them.
The key element in writing better code, though, is to understand the goal of your source code.
This might be a bit above your level, but think about it this way: Why do we write source code in higher level languages?
Why does a language support upper and lower case?
Why does a language have support for white space?
Why does a language have support for comments?
The truth is, the java compiler doesn't care about most of those things (mixed-case aside).
The reason we write in higher level languages is to make it easier for someone else (who may be you!) to understand what was INTENDED by the software. Source code is for HUMAN BEINGS to read and write.
So my advice is to make the source code as clear, easy to read, and easy to understand as you possibly can.
As h.j.k. said above, you should camel-case method and variable names, and Pascal-case type/class names. Why? Because it's the convention that's used by programmers all over the planet. It makes it easier for other people to read software you've written, if you follow the convention/style that they're used to reading.
Same rules regarding white-space, such as indentation. Matching up braces to find the blocks that are under an if/then/else is nobody's idea of a good time. Make it easy for them to find where the blocks end.
I would differ with h.j.k. on one point, though, and that's the use of multiple return statements in your Answer() method. I can certainly see his point; his way of thinking reduces the number of lines in the method, and makes it easier to see at a glance what is being returned by the method. These are all admirable goals.
However, in non-trivial software, I much prefer to see a single return statement at the bottom of the routine, because it means that I can set a single breakpoint at the return, and I can see what the function is returning in the debugger. This is a big help when developing/maintaining software written by multiple people over multiple years.
I also prefer that the local variable being returned is initialized to some known-good initial value. This guarantees the return value from the function will always be set to SOMETHING.
That's one thing you're not doing currently. You're depending on the global variable to be zero, and not explicitly setting the TheAnswer to any value.
What happens if the user enters an operator that's not a */+-?
This is becoming long winded, so I'll try show you what I think it should look like. DO NOT TURN THIS IN AS YOUR HOMEWORK. It will be very obvious to your teacher that you didn't write it. I'm not going to fix the variable declarations and such, only attempt to show you how other programmers write software.
Trust me, there's a ton more I could do to this, but I'm not fixing any of the errors you might have.
You're asking the right questions, and I'm glad to see you want to do better.
An ancient book regarding this very topic was called "Professional Pascal" by Henry Ledgard. Don't let the Pascal in the title fool you; it's about writing professional grade software, in any language.
Professional Pascal (Google Books)
I hope this helps.
** A simple console-based calculator.
** java Algorithm1
** java Algorthim1
** Give your First input please.
** Give your Second input please.
** Now * or / or + or -
public class Algorithm1
public static int FirstNumber, SecondNumber;
public static float TheAnswer;
public static String Action;
public static Scanner ScanInt1, ScanString;
public static boolean hasString;
public static boolean hasString2, hasInt;
** Compute the result of FirstNumber <operator> SecondNumber, and return the value.
public static float Answer()
TheAnswer = -1.0f; /* Initialize to some value; this should, in best practice, be a local variable. */
//This method is for the math
/* Multiplication */
if( Action.equals( "*" ) )
TheAnswer = FirstNumber * SecondNumber;
/* Division */
if( Action.equals( "/" ) )
TheAnswer = FirstNumber / SecondNumber;
/* Addition */
if( Action.equals( "+" ) )
TheAnswer = FirstNumber + SecondNumber;
/* Subtraction */
if( Action.equals( "-" ) )
TheAnswer = FirstNumber - SecondNumber;
** No arguments.
public static void main( String args )
hasString = false; //sets up the booleans
hasString = false;
hasInt = false;
ScanInt1 = new Scanner(System.in);
ScanString = new Scanner(System.in);
System.out.println("Give your First input please.");
while (FirstNumber < 1)
FirstNumber = ScanInt1.nextInt();
System.out.println("Give your Second input please.");
hasString = true;
if (SecondNumber < 1)
SecondNumber = ScanInt1.nextInt();
System.out.println("Now * or / or + or -");
hasString2 = true;
Action = ScanString.nextLine();