Write a program that does the following: Get the user\'s input. if the first num
ID: 3544421 • Letter: W
Question
Write a program that does the following:
Get the user's input.
if the first number is not 0:
Find the value of the expression
Display the value.
The input:
Assume that the user input is ALWAYS in the form of
<integer><operation><integer>
As an example 3*4
23-6
333/3
4+1200
The output:
You should display the output as:
"The result of 3*4 is 12" (The first example above)
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class driver
{
public static void main (String[] args)
{
int m,n;
Scanner scan = new Scanner(System.in);
while(true)
{
String s = scan.nextLine();
StringTokenizer stc = new StringTokenizer(s,"+-/*");
StringTokenizer stn = new StringTokenizer(s,"0123456789");
m = new Integer(stc.nextToken("+-/*"));
if(m==0)
return;
n = new Integer(stc.nextToken("+-/*"));
String c = stn.nextToken("0123456789");
int res=0;
if(c.equals("+"))
res = m+n;
else if(c.equals("-"))
res = m-n;
else if(c.equals("/"))
res = m/n;
else if(c.equals("*"))
res = m*n;
System.out.println("The result of "+s+" is : "+res);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.