1.7 Einführung Java: Selektion  / Introduction Java: Selection

1.7 Einführung Java: Selektion / Introduction Java: Selection


Um in Java eine Auswahl zu realisieren nutzen wir die if Anweisung, sie wertet aus ob etwas zutrifft.

Dazu benötigen wir die Vergleichs-Operatoren:

< kleiner als…
> grösser als…
!= ungleich wie…
== gleich wie…
<= kleiner gleich wie…
>= grösser gleich wie…


Wenn nichts in der Auswahl zutrifft nutzen wir in Java die else Anweisung.

Beispiel:

int alter = 30;

if (alter < 16) {
System.out.println("Du bist ein jugendlicher.");
}else {
System.out.println("Du bist erwachsen.");
}

Die verschachtelte if Anweisung.

Beispiel:

int alter = 25;

if (alter < 0) {

if(alter > 17) {
System.out.println("Du bist erwachsen.");

}else {
System.out.println("Du bist ein jugendlicher.");
}
}else {
System.out.println("Error");

}


Die else if Anweisung.

int alter = 25;

if(alter <= 0) {
System.out.println(“Error");
}else if (alter <= 16 ){
System.out.println(“Du bist jugendlich.");
}else if (alter < 100 ){
System.out.println(“Du bist erwachsen.");
}else {
System.out.println(“Du bist ein Kind.")
}

Um eine Variable auf verschiedene werte hin ab zu fragen nutzen wir die switch case Anweisung.

Beispiel:

switch ( auswahl ) {

case 1:
System.out.println(“Sie haben eine 1 eingegeben.“);
break;

case 2:
System.out.println(“Sie haben eine 2 eingegeben.“);
break;

default:
System.out.println(“Sie haben weder 1 noch 2 eingegeben.“);
break;


}

English:

 

To realize a selection in Java we use the if statement, which evaluates if something applies.

For this we need the comparison operators:

< smaller than...
> larger than...
!= not equal to...
== same as...
<= less than or equal to...
>= greater than or equal to...


If nothing in the selection applies we use the else statement in Java.

Example:

int age = 30;

if (age < 16) {
   System.out.println("You're a teen.");
}else {
   System.out.println(" You're an adult.");

}

The nested if statement.

Example:

int alter = 25;

if (age < 0) {

if(age > 17) {
   System.out.println(" You are an adult");

}else {
   System.out.println("You're a teenager");
}
}else {
   System.out.println("Error");

}


The else if statement.

int alter = 25;

if(alter <= 0) {
   System.out.println("Error");
}else if (old <= 16 ){
   System.out.println("You're a teen");
}else if (old < 100 ){
   System.out.println("You're an adult");
}else {
   System.out.println("You are a child")
}

 

To query a variable for different values we use the switch case statement.

Example:

switch ( selection ) {

case 1:
System.out.println("You entered a 1.");
break;

case 2:
System.out.println("You entered a 2.");
break;

default:
System.out.println("You entered neither 1 nor 2.");
break;


}

 

 

How do you rate this article?

0


Conschdi
Conschdi

Im a human. https://bittubers.com/profile/Beautiful-World Peace & Love to everyone.


App entwicklung mit Android Studio in Java
App entwicklung mit Android Studio in Java

In diesem Blog erkläre ich schritt für schritt wie man eigene Apps mit Android Studio in der Programmiersprache Java eigenhändig programmiert. In this blog I explain step by step how to program your own apps with Android Studio in the programming language Java.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.