Switch Expressions

Java:

package switchExpressions;

import javax.swing.JOptionPane;

public class Moeda {

	public static void main(String[] args) {
		
		try {
			
			double valor = Double.parseDouble(JOptionPane.showInputDialog("Valor"));
			int moeda = Integer.parseInt(JOptionPane.showInputDialog("Moeda (1,2,3)"));

			JOptionPane.showMessageDialog(null,
					switch(moeda) {
						case 1 -> String.format("Valor em Libras: %.2f", valor / 5);						
						case 2 -> String.format("Valor em Dólar: %.2f", valor / 4);
						case 3 -> String.format("Valor em Euro: %.2f", valor / 4.4);
						default -> "Moeda inválida";
			});
			
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null, "Formato inválido");
		}		

	}

}

Last updated