Java Reference
In-Depth Information
b = b + 3;
}
if (b < a) {
b++;
} else {
a--;
}
System.out.println(a + " " + b);
}
What output is produced for each of the following calls?
a. ifElseMystery2(10, 2);
b. ifElseMystery2(3, 8);
c. ifElseMystery2(4, 4);
d. ifElseMystery2(10, 30);
5. Write Java code to read an integer from the user, then print even if that number is an even number or odd otherwise.
You may assume that the user types a valid integer.
6. The following code contains a logic error:
Scanner console = new Scanner(System.in);
System.out.print("Type a number: ");
int number = console.nextInt();
if (number % 2 == 0) {
if (number % 3 == 0) {
System.out.println("Divisible by 6.");
} else {
System.out.println("Odd.");
}
}
Examine the code and describe a case in which the code would print something that is untrue about the number that
was entered. Explain why. Then correct the logic error in the code.
7. Describe a problem with the following code:
Scanner console = new Scanner(System.in);
System.out.print("What is your favorite color?");
String name = console.next();
if (name == "blue") {
System.out.println("Mine, too!");
}
8. The following code is poorly structured:
int sum = 1000;
Scanner console = new Scanner(System.in);
System.out.print("Is your money multiplied 1 or 2 times? ");
Search WWH ::




Custom Search