94 lines
2.5 KiB
Java
94 lines
2.5 KiB
Java
import java.util.Scanner;
|
|
|
|
public class OpgaverEftermiddag {
|
|
|
|
public static void main(String[] args) {
|
|
Scanner console = new Scanner(System.in);
|
|
Opgave1.opgave1(console);
|
|
Opgave2.opgave2(console);
|
|
Opgave4.opgave4(console);
|
|
}
|
|
}
|
|
|
|
class Opgave1 {
|
|
|
|
public static void opgave1(Scanner console) {
|
|
int running = 1;
|
|
double sum = 0;
|
|
double amount = 0;
|
|
System.out.println(
|
|
"Indtast tal, der skal tages gennemsnit fra. Indtast \"-1\" for at stoppe."
|
|
);
|
|
while (running == 1) {
|
|
System.out.print("Indtast tal: ");
|
|
double input = console.nextInt();
|
|
if (input == -1) {
|
|
break;
|
|
}
|
|
sum += input;
|
|
amount++;
|
|
}
|
|
double average = sum / amount;
|
|
System.out.println("Gennemsnittet blev " + average);
|
|
}
|
|
}
|
|
|
|
class Opgave2 {
|
|
|
|
public static void opgave2(Scanner console) {
|
|
System.out.print("Indtast Brugernavn: ");
|
|
String brugernavn = console.nextLine();
|
|
if (brugernavn.equals("admin") && tjekAdgangskode(console)) {
|
|
System.out.println("Adgang givet");
|
|
} else {
|
|
System.out.println("Adgang nægtet");
|
|
}
|
|
}
|
|
|
|
public static boolean tjekAdgangskode(Scanner console) {
|
|
System.out.print("Indtast adgangskode: ");
|
|
String adgangskode = console.nextLine();
|
|
|
|
return adgangskode.equals("wKzsP9A\\JWY!");
|
|
}
|
|
}
|
|
|
|
class Opgave3 {
|
|
// Laves i Python, se Opgaver.py-filen
|
|
}
|
|
|
|
class Opgave4 {
|
|
|
|
public static void opgave4(Scanner console) {
|
|
System.out.println("Tell me about the person applying for a loan.");
|
|
int age = get_number(console, "What age is the person? ");
|
|
int money = get_number(
|
|
console,
|
|
"How many dollars does the person have? "
|
|
);
|
|
|
|
boolean working_age = (age >= 18 && age < 70);
|
|
boolean rich = (money >= 100000);
|
|
|
|
if (working_age && rich) {
|
|
System.out.println("Loan granted.");
|
|
} else {
|
|
System.out.println("Loan denied");
|
|
}
|
|
}
|
|
|
|
public static int get_number(Scanner console, String prompt) {
|
|
System.out.print(prompt);
|
|
while (!console.hasNextInt()) {
|
|
console.next();
|
|
System.out.println("Not an integer; try again.");
|
|
System.out.print(prompt);
|
|
}
|
|
return console.nextInt();
|
|
}
|
|
}
|
|
|
|
class Opgave5 {
|
|
// Laves i Python. Kig i Opgaver.py-filen
|
|
}
|