Da un paio di giorni ho iniziato ad approfondire Java ed eccovi un primo script per prendere confidenza col linguaggio…
import javax.swing.JOptionPane;
public class Calcolatrice
{
public static void main(String args[])
{
String c;
String firstnum;
String secondnum;
int choice;
int first;
int second;
int tot;
choice=99;
first=0;
second=0;
while(choice!=0){
System.out.println("");
System.out.println("1-Addizione");
System.out.println("2-Sottrazione");
System.out.println("3-Divisione");
System.out.println("4-Moltiplicazione");
System.out.println("0-Esci");
System.out.println("");
c=JOptionPane.showInputDialog("Scegli: ");
choice=Integer.parseInt(c);
if (choice!=0 && choice<=4)
{
firstnum=JOptionPane.showInputDialog("Inserisci primo numero: ");
secondnum=JOptionPane.showInputDialog("Inserisci secondo numero: ");
first=Integer.parseInt(firstnum);
second=Integer.parseInt(secondnum);
}
if(choice==1)
{
tot=first+second;
System.out.println("Il totale e': "+tot);
}
else
if(choice==2)
{
tot=first-second;
System.out.println("Il totale e': "+tot);
}
else
if(choice==3)
{
tot=first/second;
System.out.println("Il totale e': "+tot);
}
else
if(choice==4)
{
tot=first*second;
System.out.println("Il totale e': "+tot);
}
else
if(choice==0)
System.exit(0);
}
}//while
}