Skip to content

Solution for Cases 04 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: exercises/cases/04
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Exercise.java
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,31 @@
import java.util.Random;
import java.util.Scanner;

public class Exercise { public class Exercise {


public static void main(String[] args) { public static void main(String[] args) {
// implement exercise here @SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);

System.out.print("Spieler 1, gib bitte Deinen Tipp ein: ");
int tip1 = sc.nextInt();

System.out.print("Spieler 2, gib bitte Deinen Tipp ein: ");
int tip2 = sc.nextInt();

// int randomNumber = (int) (Math.random() * 100);
Random rnd = new Random();
int randomNumber = rnd.nextInt(100) + 1;

int distance1 = Math.abs(randomNumber - tip1);
int distance2 = Math.abs(randomNumber - tip2);

if (distance1 > distance2) {
System.out.println("Zufallszahl: " + randomNumber + ", Spieler 2 gewinnt");
} else if (distance2 > distance1) {
System.out.println("Zufallszahl: " + randomNumber + ", Spieler 1 gewinnt");
} else {
System.out.println("Zufallszahl: " + randomNumber + ", beide Spieler gewinnen");
}
} }
} }