F Semester Java Flashcards
How do we accept user input?
we go to the top and we write
import java.util.Scanner;
then we go back and write Scanner
scanner = new Scanner (System.in);
System.out.println (“Please enter your …”);
and then we store it with
String name = scanner.nextLine();
Condition for stopping while loop
Between ()
Example:
While (kleinerZahl!= 0) {………..
……}
Checken ob 6 eine Primzahl ist
i = 1 → 6 % 1 == 0 → sum = 1
i = 2 → 6 % 2 == 0 → sum = 1 + 2 = 3
i = 3 → 6 % 3 == 0 → sum = 3 + 3 = 6
i = 4 → 6 % 4 != 0 → skip
i = 5 → 6 % 5 != 0 → skip
int sum = 0;
for (int i = 1; i < 6; i++) {
if (6 % i == 0) {
sum += i;
}
}
Calculate how much time the program takes to run?
long begin = System.currentTimeMillis();
CODE
long end = System.currentTimeMillis();
long time= (end-begin)/1000;
System.out.println(“Elapsed Time : “ + time + “ milliseconds.”);