delifert.blogg.se

Simple console calculator in java
Simple console calculator in java







simple console calculator in java

So this: ("=") Ĭould become this: StringBuilder stringBuilder = new StringBuilder() On the other hand, StringBuilder is very efficient. It doesn't matter very much for this program, but it could matter a lot when you are working on something more complicated later on. Repeatedly calling is very resource intensive. Using abbreviations like this is almost never worth the characters that you will save. In this case I would recommend simply calling the variables firstNumber and secondNumber. You should make a strong effort to make your variable names as descriptive as possible. These are bad variable names: int fnum = 0 Unfortunately it is helpful for the wrong reasons. On the other hand, the comment that you have for fnum, snum, and answer is somewhat helpful. It is completely obvious that a new scanner object is being created, and that the while loop will keep the program running until the user exits.

#Simple console calculator in java code#

Your comments should typically explain Why you have done something, and almost never explain What your code is doing. Since this is a beginner program you can be excused for the very bad comments, but you still need to hear about them.Ĭomments like this: // Creates scanner objectĪre all pretty much useless. You must always test for division by 0 in cases like this: snum = scanner.nextInt() What happens when the user enters 0? Your program will crash.

simple console calculator in java simple console calculator in java

Your division operation has a bug: snum = scanner.nextInt() Using case Operation.Addition: is clearer than case 1:, and there is a very limited set of data that allowed, which is demonstrated and enforced by the enum, which only accepts the allowed set, and is not enforced by an int. EnumsĪnother thing you should look into is using an enum to designate the selected operation. For example, you could create a method for input, a method for calculating the answer, and a method for output. This is a pretty straightforward program, but you should start separating unique components of your program and creating more methods. Will display the same as this: (" Calculator V1.0") This really is a nitpick for now, but this: (" Calculator V1.0 ") Most of that can be moved to before or after the switch: ("Enter first number: ") If you are using the comment to remember what the code does, not just because some teacher/book said to, a comment here is perfectly fine: // Choose action based on inputĪnything catch your eye there? Lots and lots of copy/pasted code. A comment should say why the code does something if it is not clear and the code should say what it does (if the code is unclear, you can use a comment, but this is also a signal you should write clearer code), this comment is just plain noise. ("Invalid operation!") Ĭomments // Does stuff depending on what integer the user enters Does stuff depending on what integer the user enters While loop that keeps the program running until user exits Sets and resets the operand and answer variables Controls the start and stop of the program Scanner scanner = new Scanner(System.in) Tell me what's good and what I can improve on. I just started learning a week ago so any feedback (positive and negative) is welcome.









Simple console calculator in java