Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Uso de If Anidados Wicth
Uso de If Anidados Wicth
if-else-if
Flujograma de If anidado
if-else Matching
if-else Matching
Este else
va con este
if.
Este else
va con este
if.
if (employed == y)
{
if (recentGrad == y)
{
System.out.println("You qualify for the " +
"special interest rate.");
}
else
{
System.out.println("You must be a recent " +
"college graduate to qualify.");
}
}
else
{
System.out.println("You must be employed to qualify.");
}
Operadores Lgicos
Operadores lgicos
Operador
&&
||
!
Significado
Efecto
AND
OR
NOT
Expresin 2
true
false
false
false
true
false
false
false
false
true
true
true
Expresin 2
Expresin1 | | Expresin2
true
false
true
false
true
true
false
false
false
true
true
true
!Expresin1
true
false
false
true
Order of Precedence
Orden de
Precedencia
Operadores
( negacin) !
* / %
+ -
== !=
&&
||
= += -=
*= /= %=
El operador condicional: ?
Por ejemplo:
System.out.println(your grade is: +
(score> 60 ? Fail. : Pass.));
x > y ? z = 10 : z = 5;
Ejemplo de switch
import java.util.Scanner;
public class SwitchDemo{
public static void main(String[] args){
int number;
// A number entered by the user
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter 1, 2, or 3: ");
number = keyboard.nextInt();
// Determine the number entered.
switch (number)
{
case 1:
System.out.println("You entered 1.");
break;
case 2:
System.out.println("You entered 2.");
break;
case 3:
System.out.println("You entered 3.");
break;
default:
System.out.println("That's not 1, 2, or 3!");
}
}
}
Switch
switchcase
Ejemplo de comparacin de
cadenas de caracteres
public class StringCompare{
public static void main(String [] args){
String name1 = "Mark",
name2 = "Mark",
name3 = "Mary";
// Compare "Mark" and "Mark"
if (name1.equals(name2))
System.out.println(name1 + " and " + name2 +
" are the same.");
else
System.out.println(name1 + " and " + name2 +
" are the NOT the same.");
// Compare "Mark" and "Mary"
if (name1.equals(name3))
System.out.println(name1 + " and " + name3 +
" are the same.");
else
System.out.println(name1 + " and " + name3 +
" are the NOT the same.");
}
}
import java.util.Scanner;
public class SecretWord{
public static void main(String[] args){
String input; // To hold the user's input
Scanner keyboard = new Scanner(System.in);
// Prompt the user to enter the secret word.
System.out.print("Enter the secret word: ");
input = keyboard.nextLine();
// Determine whether the user entered the secret word.
if (input.equalsIgnoreCase("PROSPERO"))
System.out.println("Congratulations! You know the " +
"secret word!");
else
System.out.println("Sorry, that is NOT the " +
"secret word!");
}
}