Javakurs/Übungsaufgaben/Glücksspiel/Musterloesung: Unterschied zwischen den Versionen
< Javakurs | Übungsaufgaben | Glücksspiel
(Die Seite wurde neu angelegt: „==42%== <code> public class FortyTwo{ public static void main(String[] args){ double randomValue = Math.random(); if(randomValue <= 0.42){ …“) |
K (Update alignment / formatting) |
||
| Zeile 1: | Zeile 1: | ||
==42%== | ==42%== | ||
| − | < | + | <pre> |
| − | + | public class FortyTwo { | |
| − | + | public static void main(String[] args) { | |
| − | + | double randomValue = Math.random(); | |
| − | + | ||
| − | + | if (randomValue <= 0.42) { | |
| − | + | System.out.println("Gewonnen!"); | |
| − | + | } else { | |
| − | + | System.out.println("Verloren!"); | |
| − | + | } | |
| − | + | } | |
| − | + | } | |
| − | + | </pre> | |
| − | |||
| − | </ | ||
==Begrüßung== | ==Begrüßung== | ||
| − | < | + | <pre> |
| − | + | public class Welcome { | |
| − | + | public static void main(String[] args) { | |
| − | + | int age = 23; | |
| − | + | String name = "Meier"; | |
| − | + | boolean male = true; | |
| − | + | ||
| − | + | if (age >= 18) { | |
| − | + | if (male) { | |
| − | + | System.out.println("Guten Tag Herr " + name + "!"); | |
| − | + | } else { | |
| − | + | System.out.println("Guten Tag Frau " + name + "!"); | |
| − | + | } | |
| − | + | } else { | |
| − | + | System.out.println("Hallo " + name + "!"); | |
| − | + | } | |
| − | + | } | |
| − | + | } | |
| − | + | </pre> | |
| − | |||
| − | </ | ||
==Glückspiel== | ==Glückspiel== | ||
| − | < | + | <pre> |
| − | + | public class Gamble { | |
| − | + | public static void main(String[] args) { | |
| − | + | double playerOne = 40.0; | |
| − | + | double playerTwo = 60.0; | |
| − | + | ||
| − | + | double randomValue = Math.random() * 100.0; | |
| − | + | ||
| − | + | // Math.abs() ist eine Methode die den Betrag einer Zahl zurueck gibt. | |
| − | + | double spacingOne = Math.abs(playerOne - randomValue); | |
| − | + | double spacingTwo = Math.abs(playerTwo - randomValue); | |
| − | + | ||
| − | + | if (spacingOne < spacingTwo) { | |
| − | + | System.out.println("Spieler Eins hat gewonnen!"); | |
| − | + | } | |
| − | + | ||
| − | + | if (spacingOne > spacingTwo) { | |
| − | + | System.out.println("Spieler Zwei hat gewonnen!"); | |
| − | + | } | |
| − | + | ||
| − | + | if (spacingOne == spacingTwo) { | |
| − | + | System.out.println("Unentschieden!"); | |
| − | + | } | |
| − | + | } | |
| − | + | } | |
| − | + | </pre> | |
| − | |||
| − | </ | ||
Aktuelle Version vom 27. Februar 2013, 16:54 Uhr
42%
public class FortyTwo {
public static void main(String[] args) {
double randomValue = Math.random();
if (randomValue <= 0.42) {
System.out.println("Gewonnen!");
} else {
System.out.println("Verloren!");
}
}
}
Begrüßung
public class Welcome {
public static void main(String[] args) {
int age = 23;
String name = "Meier";
boolean male = true;
if (age >= 18) {
if (male) {
System.out.println("Guten Tag Herr " + name + "!");
} else {
System.out.println("Guten Tag Frau " + name + "!");
}
} else {
System.out.println("Hallo " + name + "!");
}
}
}
Glückspiel
public class Gamble {
public static void main(String[] args) {
double playerOne = 40.0;
double playerTwo = 60.0;
double randomValue = Math.random() * 100.0;
// Math.abs() ist eine Methode die den Betrag einer Zahl zurueck gibt.
double spacingOne = Math.abs(playerOne - randomValue);
double spacingTwo = Math.abs(playerTwo - randomValue);
if (spacingOne < spacingTwo) {
System.out.println("Spieler Eins hat gewonnen!");
}
if (spacingOne > spacingTwo) {
System.out.println("Spieler Zwei hat gewonnen!");
}
if (spacingOne == spacingTwo) {
System.out.println("Unentschieden!");
}
}
}