true
to false
and vice versa:
boolean b;
…
// b is assigned a value (true or false)
…
b = !b; // toggles b
for example:
boolean isOn = true;
…
isOn = !isOn;
isFirst
— initialized to true
which is toggled to false
after the first iteration.
boolean isFirst = true; for (.......) { if (isFirst) { … // do whatever needs doing the first time only isFirst = false; // makes sure we never do this again } …
The positive numbers are: … …If there are no positive numbers, nothing (including the prefixing title) should be printed.
import java.io.*; import java.util.*; public class PrintPositives { public static void main(String [] args) throws Exception { Scanner scanner = new Scanner(new File("numbers.text")); boolean isFirst = true; int n = scanner.nextInt(); for (int i = 1; i <= n; i++) { int num = scanner.nextInt(); if (num > 0) { if (isFirst) { System.out.println("The positive numbers are: "); isFirst = false; } System.out.println(num); } } } }
, 1, 2, 3
1, 2, 3,
boolean isFirst = true;
for (.......) {
if (isFirst)
isFirst = false; // don't want to do anything first time, but we have to toggle isFirst
else {
… // all the other times, do what needs to be done
}
…
isFirst
boolean variable is in the same place (within the true part of the if (isFirst)
conditional), but the code to be performed is now in the false part (since we want it to be done everytime except the first.
import java.util.*; public class CommaLogic { public static void main(String [] args) throws Exception { Scanner keyboard = new Scanner(System.in); System.out.println("Print from 1 to wwhere? "); int n = keyboard.nextInt(); boolean isFirst = true; for (int i = 1; i <= n; i++) { if (isFirst) isFirst = false; else System.out.print(", "); System.out.print(i); } } }
int x, y; // x and y are assigned values int temp = x; x = y; y = temp;
1 2 3 → 3 1 2 // Each variable's value move to the variable to its right; least one comes around
1 2 3 → 3 1 2 // right rotation 1 2 3 → 2 3 1 // left rotation
1 2 3 → 0 1 2 // right shift with 0 fill 1 2 3 → 2 3 0 // left shift with 0 fill
public class BruteForceMax { public static void main(String [] args) { System.out.println(max(2, 3, 1)); } public static int max(int x, int y, int z) { int max; if (x >= y && x >= z) max = x; else if (y >= x && y >= z) max = y; else max = z; return max; } }
public class SemiBruteForceMax { public static void main(String [] args) { System.out.println(max(2, 3, 1)); } public static int max(int x, int y, int z) { int max; if (x > y) if (x > z) max = x; else max = z; else if (y > z) max = y; else max = z; return max; } }
public class CascadedMax { public static void main(String [] args) { System.out.println(max(2, 3, 1)); } public static int max(int x, int y, int z) { int max = x; if (y > max) max = y; if (z > max) max = z; return max; } }
public class CompositionMax { public static void main(String [] args) { System.out.println(max(2, 3, 1)); } public static int max(int x, int y, int z) { return max(max(x, y), z); } public static int max(int x, int y) { if (x > y) return x; return y; } }