Scelta multipla

Una struttura molto comune per i programmi di prova.

Un menu con opzioni

  • 0, per uscire
  • 1
  • n, le opzioni disponibili.
import javax.swing.JOptionPane;
class Argomento
{
   public static void main(String[] args)
   {
      String menu="........... Argomento ........... \n" +
                  "0. Uscita                         \n" +
                  "1. Opzione 1                      \n" +
                  "2. Opzione 2                      \n" +
                  "............                      \n" +
                  "Cosa scegli?                      \n";
      String stringa;
      int    scelta;
      do
      {
         stringa=JOptionPane.showInputDialog(menu);
         scelta =Integer.parseInt(stringa);
         switch(scelta)
         {
            case  0: // .... USCITA ..................
                     break;
            case  1: // .... OPZIONE 1 ...............
                     break;
            case  2: // .... OPZIONE 2 ...............
                     break;
            // .......................................
            default: // .... OPZIONE NON PREVISTA ....
                     break;
         }
      }
      while(scelta != 0);
   }
}

Note

    1. switch(scelta) { ... }
    2. do{ ... } while(scelta != 0);