What's new

Java simple calcu

Phc-AJDS

Eternal Poster
Joined
Feb 26, 2017
Posts
718
Reaction
905
Points
356
import java.util.Scanner;
public class SimpleCalculator
public static void mainM(String[] args) {
Scanner input=new Scanner(System.in);
int fnum,snum,ans;
char sign;
System.out.println("Welcome User,This is a Simple Calculator Created by Victor Using the if statement");

System.out.print("Please Enter your first digit: ");

fnum=input.nextInt();
System.out.print("Enter the second digit: ");
snum=input.nextInt();
System.out.print("Enter the mathematical operator to be used: "); //Ask the user to input the mathematical operator to be used
sign=input.next().charAt(0); //Receive input from user
if (sign == '+' )/making use of if statement to determine the addition sign/{
ans=fnum + snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else if(sign == '-')/* else if statement for minus sign*/
{
ans=fnum-snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else if(sign == '/'){
ans=fnum/snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else if(sign == '*'){
ans=fnum*snum;
System.out.println(fnum +" "+sign +" "+snum +"= "+" "+ ans );
}
else

System.out.println("Your Input is not correct,please check it for any error(s).");



}

}
ano po kayang mali dito?
 
Java:
import java.util.Scanner;
public class SimpleCalculator
    public static void main(String[] args) {
  
        Scanner input = new Scanner(System.in);
      
        int firstNum,secondNum,totalAns;
        char sign;
 
        System.out.println("Welcome User,This is a Simple Calculator Created by Victor Using the if statement");

        System.out.print("Please Enter your first digit: ");
            firstNum=input.nextInt();

        System.out.print("Enter the second digit: ");
            secondNum=input.nextInt();
      
        System.out.print("Enter the mathematical operator to be used: ");
            //Ask the user to input the mathematical operator to be used
            sign=input.next().charAt(0); //Receive input from user


        if(sign == '+' ){ //making use of if statement to determine the addition sign/
            totalAns = firstNum + secondNum;
            System.out.println(firstNum +" "+sign +" "+secondNum +"= "+" "+ totalAns );
        }
        else if(sign == '-')/* else if statement for minus sign*/
        {
            totalAns = firstNum - secondNum;
            System.out.println(firstNum +" "+sign +" "+secondNum +"= "+" "+ totalAns );
        }
        else if(sign == '/')
        {
            totalAns = firstNum / secondNum;
            System.out.println(firstNum +" "+sign +" "+secondNum +"= "+" "+ totalAns );
        }
        else if(sign == '*')
        {
            totalAns = firstNum * secondNum;
            System.out.println(firstNum +" "+sign +" "+secondNum +"= "+" "+ totalAns );
        }
        else{
            System.out.println("Your Input is not correct,please check it for any error(s).");
        }
      
    }
}
 

Similar threads

Back
Top