1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
package Textprg;
import java.util.Scanner;
public class Calculteur
{
public static void main (String[] args)
{
double firstnb;
double secondnb;
String operator;
double value;
System.out.println("Entrez expressions suc as 2+2
or 34.2*7.81");
System.out.println("To end ,enter a 0");
while(true)
{
System.out.println("Entrez votre premier Nb:");
Scanner sc=new Scanner(System.in);
firstnb=sc.nextInt();
if(firstnb==0)
break;
System.out.println("operator");
Scanner sc1=new Scanner(System.in);
operator=sc1.nextLine();
System.out.println("Entrez second Nb");
Scanner sc2=new Scanner(System.in);
secondnb=sc2.nextInt();
int oper = 4;
if (operator.equalsIgnoreCase("+")) oper = 0;
else
if (operator.equalsIgnoreCase("-")) oper = 1;
else
if (operator.equalsIgnoreCase("*")) oper = 2;
else
if (operator.equalsIgnoreCase("/")) oper = 3;
switch(oper)
{
case 0:
value = firstnb + secondnb ;
break;
case 1:
value = firstnb - secondnb ;
break;
case 2:
value = firstnb * secondnb ;
break;
case 3:
value = firstnb / secondnb ;
break;
default:
System.out.println("Unknown operator: " + operator);
continue;
}
System.out.println("Value is " + value);
System.out.println();
}
System.out.println("Good bye");
}
} |