Zum Inhalt springen
Sitzung: Jeden Freitag in der Vorlesungszeit ab 16 Uhr c. t. im MAR 0.005. In der vorlesungsfreien Zeit unregelmäßig (Jemensch da?). Macht mit!

Javakurs/Übungsaufgaben/Glücksspiel/Musterloesung: Unterschied zwischen den Versionen

Aus Freitagsrunde
Die Seite wurde neu angelegt: „==42%== <code> public class FortyTwo{ public static void main(String[] args){ double randomValue = Math.random(); if(randomValue <= 0.42){ …“
 
MarkusH (Diskussion | Beiträge)
K Update alignment / formatting
 
Zeile 1: Zeile 1:
==42%==
==42%==
<code>
<pre>
public class FortyTwo{
public class FortyTwo {
public static void main(String[] args) {
public static void main(String[] args){
double randomValue = Math.random();
 
double randomValue = Math.random();
if (randomValue <= 0.42) {
System.out.println("Gewonnen!");
if(randomValue <= 0.42){
} else {
System.out.println("Gewonnen!");
System.out.println("Verloren!");
}else{
}
System.out.println("Verloren!");
}
}
}
}
</pre>
}
</code>


==Begrüßung==
==Begrüßung==
<code>
<pre>
public class Welcome{
public class Welcome {
public static void main(String[] args) {
public static void main(String[] args){
int age = 23;
String name = "Meier";
int age = 23;
boolean male = true;
String name = "Meier";
 
boolean male = true;
if (age >= 18) {
if (male) {
if(age>=18){
System.out.println("Guten Tag Herr " + name + "!");
if(male){
} else {
System.out.println("Guten Tag Herr "+name+"!");
System.out.println("Guten Tag Frau " + name + "!");
}else{
}
System.out.println("Guten Tag Frau "+name+"!");
} else {
}
System.out.println("Hallo " + name + "!");
}else{
}
System.out.println("Hallo "+name+"!");
}
}
}
}
</pre>
}
</code>


==Glückspiel==
==Glückspiel==
<code>
<pre>
public class Gamble{
public class Gamble {
public static void main(String[] args) {
public static void main(String[] args){
double playerOne = 40.0;
double playerTwo = 60.0;
double playerOne = 40.0;
 
double playerTwo = 60.0;
double randomValue = Math.random() * 100.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);
//Math.abs() ist eine Methode die den Betrag einer Zahl zurueck gibt.
double spacingTwo = Math.abs(playerTwo - randomValue);
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 Eins hat gewonnen!");
 
}
if (spacingOne > spacingTwo) {
System.out.println("Spieler Zwei hat gewonnen!");
if(spacingOne > spacingTwo){
}
System.out.println("Spieler Zwei hat gewonnen!");
 
}
if (spacingOne == spacingTwo) {
System.out.println("Unentschieden!");
if(spacingOne == spacingTwo){
}
System.out.println("Unentschieden!");
}
}
}
}
</pre>
}
</code>

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!");
		}
	}
}