You should use a simple loop mechanism outside of the method, so that kalkulator can return quickly and normally for its next invocation by the loop. For example:. While I think it's helpful that the other answers are pointing you towards an enum -driven approach, for a beginner, I feel starting with basic String comparison is I wouldn't change much here, but I'll suggest renaming your String In variable in scanOperator Scanner to something like input to stick to the camelCase convention you have followed elsewhere.
On a related note to the previous section, since you shouldn't be checking for the end as an operator here, you can safely drop the check. This is to promote better understanding of your code by others who may not understand your native written language.
Nice improvement is to use Enum with behaviour. This is an example from Effective Java book :. To fix the last point, you could handle the default state by throwing an exception or something like that, or better you could create an operator class that contains the whole logic for an operator. For now, you can think of an enumeration as a useful way to store related series of values that won't change.
A calculator is a good opportunity to look into them rather than referencing the values directly, and if you're curious there's an interesting way we can apply them to the purpose of a calculator. Notice that I optionally directly accept argument which is something desirable for a CLI. This example has a lot you can still improve on, including input validation and a more direct utilization of the enumeration among other things. Sign up to join this community. The best answers are voted up and rise to the top.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. A simple command-line calculator Ask Question. Asked 5 years, 8 months ago. Active 5 years, 8 months ago. Viewed 9k times. One of the things that was changed over time was the use of the Scanner. Firstly I had it create a new one on each iteration of kalkulator ; but that was obviously a unnecessary waste. Please try again. Improve this question. Vepir Vepir 2 2 silver badges 11 11 bronze badges.
Add a comment. Active Oldest Votes. Type end if so. Improve this answer. Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. I have written a code for a command-line calculator in Java but I can't understand the errors which are not allowing it to compile. You cannot define methods inside your main method, so this is most likely one of your first programs in Java.
Nothing wrong with that, but this makes you an example of the target audience for most introductory texts for programming in Java if you do not have a teacher. The Oracle Java Tutorial is quite nice. You probably mean System. And in the parameters there is no "field sustitution" as is C printf , but you need to mount the expression:. Last that I can see at first glance , you must define the functions that you call outside of main but inside a class.
Also you need either to declare them static as main or create a new object of this class in order to access them from main. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Simple Command Line calculator Ask Question.
Asked 10 years, 6 months ago. Active 10 years, 6 months ago. Viewed 13k times. Cipher Cipher 1 1 1 gold badge 2 2 silver badges 2 2 bronze badges. Please edit your post and add them in detail — gideon. Maybe you should start by reading a good Java Tutorial — Jacob. This is full of errors, it's not even close to being Java - I don't know where to start — skaffman. Show 5 more comments.
Active Oldest Votes. Sadly, this is likely to receive the green check. You have given this man a fish. Thanks very much, Now I understood everything — Cipher.
0コメント