3
It has how to treat x as *, because the user will type x to multiply a number, it would be like the program to recognize x as multiplication via the code?
example double b = 5 x 5;
3
It has how to treat x as *, because the user will type x to multiply a number, it would be like the program to recognize x as multiplication via the code?
example double b = 5 x 5;
6
No. The multiplication operator in Java is *
. However, you can do this for the end user.
For example, he can even type 5 x 5
as a multiplication, hence the program takes the string and replaces the x
for *
and mathematically evaluate the expression resulting from the substitution.
Let’s assume the following mathematical instructions in a Java code:
int x = 10;
int a = 10 * x; //Compila corretamente, com a recebendo o valor 100;
int b = 10 x x; //Como deve ser entendido cada x? Um é multiplicação e outro é identificador?
//Duas multiplicações?
//Dois identificadores?
The language must be uniform and precise. Otherwise, compromises occur and nobody knows why the hell the code doesn’t do what should be done.
When you receive the user input, you can change it without their knowledge and display the correct result with an expression other than their own, provided that the expression used to calculate is equivalent to the one the user gave to the program.
Example: Calculator with graphical interface
Let’s assume a traditional calculator, with numbers from 0 to 9 and only the multiplication operator (to simplify).
Each number puts its value on the display and the multiplication button puts the letter x
. What the button of =
should do?
The algorithm would be:
x
of the variable by *
. Easily done with replaceAll
, and note that it is not necessary for this new string to be shown to the user;*
surrounds and multiply;2
By the question tag, I will assume you are implementing in Java.
There are several ways to do this, the one I’m going to suggest is just one of them, not necessarily the best or simplest:
A hypothetical example using Beanshell would be:
Interpreter interp = new Interpreter();
interp.eval("b = 5 * 5");
System.out.println("resultado: b="+interpreter.get("b"));
Browser other questions tagged java
You are not signed in. Login or sign up in order to post.
When you say "user" do you mean the person using the system you created in Java or yourself programming in Java? It is unclear if you intend to change the
*
forx
when programming (i.e., in code) or evaluating the mathematical expression typed by the user in a UI. If this is the second case, Dé uma olhada neste outra pergunta: http://answall.com/questions/51324/como-calcular-express%C3%A3o-matematicas-numa-string– Luiz Vieira
Not in the Java code itself. But in its data entry, of course, it depends on how you’re handling it. Edit the question by posting your code.
– Maniero
even the most layman uses *, it makes no sense to think that he will type X...
– RodrigoBorth