In: Computer Science
Can you write code or class in java accepting from the user a + b as a string and accepting their value from the user and the out but will be the sum of the variables.
****This requires some effort so please drop a like if you are satisfied with the solution****
I have satisfied all the requirements of the question and I'm providing the screenshots of code and output for your reference...
Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter the
expression in the form of 'a+b' to evalaute: ");
String exp=in.nextLine();
int
n1=Integer.parseInt(exp.substring(0,exp.indexOf("+")));
int
n2=Integer.parseInt(exp.substring(exp.indexOf("+"),exp.length()));
System.out.println("Result:
"+(n1+n2));
}
}
Output Screenshot:
Code Screenshot: