Tabla de contenidos
Sobre la marcha e interactivamente entre todos, con unos minutos para ir haciendo en parejas cada apartado y luego poniéndolo en común entre todos, repasaremos:
/* * A simple class, just to play with some basic Java statements */ public class Example { }
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { } }
shortMsg
, con valor inicial "Short Message".
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; } }
longMsg
, con valor inicial "Looooooooooong message".
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; } }
printShort
, cuyo valor inicial sea true.
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = true; } }
counter
, y no le asignes ningún valor inicial:
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = true; int counter; } }
shortMsg
sólo si printShort
vale true:
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = true; int counter; if (printShort) { System.out.println(shortMsg); } } }
printShort
a false, compila y ejecuta.
printShort
es false se imprima longMsg
:
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } } }
printShort
a true. Compila y ejecuta.
printShort
, lo siguiente que imprima el programa sea el mensaje "This message is always printed":
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; String alwaysMsg = "This message is always printed"; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } System.out.println(alwaysMsg); } }
printShort
a false. Compila y ejecuta.
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { final int NUM_QUESTIONS = 10; String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; String alwaysMsg = "This message is always printed"; String questionMsg = "Are we there yet?"; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } System.out.println(alwaysMsg); for (int i = 0; i<NUM_QUESTIONS; i++) { System.out.println(questionMsg); } } }
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { final int NUM_QUESTIONS = 5; String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; String alwaysMsg = "This message is always printed"; String questionMsg = "Are we there yet?"; String answerMsg = "Nop"; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } System.out.println(alwaysMsg); for (int i = 0; i<NUM_QUESTIONS; i++) { System.out.println(questionMsg); System.out.println(answerMsg); } } }
counter
como contador de veces:
/* * A simple class, just to play with some basic Java statements */ public class Example { public static void main (String[] args) { final int NUM_QUESTIONS = 5; final int NUM_SELFMSG = 7; String shortMsg = "Short Message"; String longMsg = "Looooooooooong message"; boolean printShort = false; int counter; String alwaysMsg = "This message is always printed"; String questionMsg = "Are we there yet?"; String answerMsg = "Nop"; String selfMsg = "I'm going to pass this course."; if (printShort) { System.out.println(shortMsg); } else { System.out.println(longMsg); } System.out.println(alwaysMsg); for (int i = 0; i<NUM_QUESTIONS; i++) { System.out.println(questionMsg); System.out.println(answerMsg); } counter = 0; while (counter < NUM_SELFMSG) { System.out.println(selfMsg); counter++; } } }
Haz los siguientes tests de auto-evaluación: