|
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import javax.swing.JFrame; import javax.swing.JOptionPane; public class MiniJava { public static String readString(String text) { JFrame frame = new JFrame(); String s = JOptionPane.showInputDialog(frame, text); frame.dispose(); if (s == null) System.exit(0); return s; } public static String readString() { return readString("Eingabe:"); } public static int readInt(String text) { JFrame frame = new JFrame(); String s = JOptionPane.showInputDialog(frame, text); frame.dispose(); int x; if (s == null) System.exit(0); try { x = Integer.parseInt(s.trim()); } catch (NumberFormatException e) { x = readInt(text); } return x; } public static int readInt() { return readInt("Geben Sie eine ganze Zahl ein:"); } public static int read(String text) { return readInt(text); } public static int read() { return readInt(); } public static double readDouble(String text) { JFrame frame = new JFrame(); String s = JOptionPane.showInputDialog(frame, text); frame.dispose(); double x; if (s == null) System.exit(0); try { x = Double.parseDouble(s.trim()); } catch (NumberFormatException e) { x = readDouble(text); } return x; } public static double readDouble() { return readDouble("Geben Sie eine Zahl ein:"); } public static void write(String output) { JFrame frame = new JFrame(); JOptionPane.showMessageDialog(frame, output, "Ausgabe", JOptionPane.PLAIN_MESSAGE); frame.dispose(); } public static void write(int output) { write("" + output); } public static void write(double output) { write("" + output); } public static int randomInt(int minval, int maxval) { return minval + (new java.util.Random()).nextInt(maxval - minval + 1); } public static int drawCard() { return randomInt(2, 11); } public static int dice() { return randomInt(1, 6); } } |