Me again. Another stump. This time I have to recieve two inputs from the user and perform a few math operations using both those two inputs.
Meaning I have String a, b; for my two user inputs. Is it possible to get all 4 of my math operations, addition, aubtraction, multiplication, division, from just a and b or do I have to create another 6 more String variables and recieve 6 more inputs, to a total of 8 inputs from the user.

Member Avatar for iamthwee

Me again. Another stump. This time I have to recieve two inputs from the user and perform a few math operations using both those two inputs.
Meaning I have String a, b; for my two user inputs. Is it possible to get all 4 of my math operations, addition, aubtraction, multiplication, division, from just a and b or do I have to create another 6 more String variables and recieve 6 more inputs, to a total of 8 inputs from the user.

Depends what ur trying to do. Are you trying to write a basic calculator, or a clever one?

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

I'm just trying to do output the results of addition subtraction multiplication and division from only 2 intputs by the user. Meaning they were to input number 1 and number 2, say 2 and 2, then the program would output the results of both those numbers being added and subtracted and so on. Only from the two inputs.

Member Avatar for iamthwee

You mean something like this?

PLEASE ENTER TWO INPUTS  ?A:B
PLEASE ENTER AN OPERATOR ? C

IF C='+'
 THEN PRINT A+B
ENDIF

 IF C='-'
   THEN PRINT A-B
 ENDIF

 .
 .
 .

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

Not really. The user inputs 2 numbers, but the program automatically does all four operations, no choice given and prints it out for the user. I can't figure out how that is to be done, since I have 4 int variables and to convert the String inputs into int data and then used from there by the math functions.

Member Avatar for iamthwee

Not really. The user inputs 2 numbers, but the program automatically does all four operations, no choice given and prints it out for the user. I can't figure out how that is to be done, since I have 4 int variables and to convert the String inputs into int data and then used from there by the math functions.

Kiddo dat's really simple.

If you can't do that you need to go back to basics.

PLEASE ENTER TWO INPUTS  ?A:B

 PRINT A+B
 PRINT A-B
 PRINT A*B
 PRINT A/B

As far as I'm aware you don't need to convert anything to strings, unless of course you're designing a GUI? And in that case you would be converting the string into an int.

[IMG]http://img476.imageshack.us/img476/5171/cut20ln.png[/IMG]
Piworld ™
[Tis simple as Pie]

Actually I am in basics. I just started Java in college about a week ago. So I'm pretty lost when it comes to these kind of things. Well in a way you helped. Thanks. I got it to work. :)

int a = ?
int b = ?

System.out.println("Addition: "+(a+b));
System.out.println("Subtraction: "+(a-b));
System.out.println("Multiplication: "+(a*b));
System.out.println("Division: "+(a/b));

And if the user input is a string: int a = Integer.parseInt(value);

Now what's confusing?

take the input as:

int a,b;
private Scanner In=new Scanner(System.in);

System.out.println("Enter the First Number");
a=In.nextInt();

system.out.println("Enter the second number");
b=In.nextInt();

now write the methods or statements for calculating the values in various operations.

But before using Scanner class just import java.util.*;
ok!!
Try this and tell me the result!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.