Answer the question
In order to leave comments, you need to log in
How to write an unusual calculator in Java?
Hello, I'm new to Java and that's why I'm asking here. How can you make a program that will do such things: for example, the user writes "Divide 4 by 2", and she answers him "When dividing your numbers give 2". The same goes for the rest of the arithmetic. I've been sitting here for 40 minutes and I can't figure it out.
Answer the question
In order to leave comments, you need to log in
Why do you need 2 instances of Scanner for dividend and divisor? It is enough to create one instance of the class and create 2 variables for the dividend and divisor.
As for the implementation of the calculator, then everything depends on your capabilities and your imagination:
Here are a few implementation options ...
1) Use of switch case. The user enters "Divide", you use the specified construction to find the desired arithmetic operator, then ask the user to enter numbers (dividend and divisor). Well, you provide an answer. Then you need to provide an option when the user enters the "left" word and issue some informational message for this.
2) Another option, such as accepting a whole sentence from the user. String, and then using a regular expression to get the necessary information (arithmetic operator in words, dividend and divisor). Here you will need knowledge of regular expressions (regExp)
3) Another option, for example, to exclude such moments as "divide", "split", etc. get the root of the word by checking with the method contains("дел")
, and then, according to the root of the word, perform the necessary calculation. Well, or take a library that works with grammar.
Something like - wordnet.ru
4) neural networks, etc.
Probably something like this ...
Maybe there are more elegant ways to solve the issue)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question