Trust me, there's a ton more I could do to this, but I'm not fixing any of the errors you might have. I'm not going to fix the variable declarations and such, only attempt to show you how other programmers write software. It will be very obvious to your teacher that you didn't write it. This is becoming long winded, so I'll try show you what I think it should look like. What happens if the user enters an operator that's not a */+-? You're depending on the global variable to be zero, and not explicitly setting the TheAnswer to any value. That's one thing you're not doing currently. This guarantees the return value from the function will always be set to SOMETHING. I also prefer that the local variable being returned is initialized to some known-good initial value.
#Simple console calculator in java software#
This is a big help when developing/maintaining software written by multiple people over multiple years. 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. 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. on one point, though, and that's the use of multiple return statements in your Answer() method. Make it easy for them to find where the blocks end. Matching up braces to find the blocks that are under an if/then/else is nobody's idea of a good time. Same rules regarding white-space, such as indentation. 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. Why? Because it's the convention that's used by programmers all over the planet. said above, you should camel-case method and variable names, and Pascal-case type/class names.
#Simple console calculator in java code#
So my advice is to make the source code as clear, easy to read, and easy to understand as you possibly can.Īs h.j.k. Source code is for HUMAN BEINGS to read and write. 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.
The truth is, the java compiler doesn't care about most of those things (mixed-case aside). Why does a language have support for comments? Why does a language have support for white space? Why does a language support upper and lower case? This might be a bit above your level, but think about it this way: Why do we write source code in higher level languages? The key element in writing better code, though, is to understand the goal of your source code. There are excellent suggestions in the above text, and I'd echo most of them. Public static boolean hasString2, hasInt Public static Scanner ScanInt1, ScanString Public static int FirstNumber, SecondNumber
Just so you know i am a real beginner just got into java about a couple of weeks ago. I just want to know if it can be improved in any way. This is my Java 8 code for a console calculator.