Files
uni/second/semester2/CT2109/Assignments/Assignment-01/code/newAlphabet.java
2023-12-07 01:19:12 +00:00

108 lines
4.3 KiB
Java

import java.util.*;
import java.io.*;
public class Alphabet {
// declaring a Scanner object to use to scan in the user input
public static Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
System.out.println("Type the alphabet in order (lowercase).");
// prompting the user to enter 'f' or 'b' and scanning in the input (only scanning the 0th character of input)
System.out.println("Forwards or Backwards? (f/b): ");
char fb = scanner.next().charAt(0);
// switch statement to determine forward or backwards, or exit
switch(fb) {
case 'f':
boolean forwards = true;
break;
case 'b':
boolean forwards = false;
break;
default:
System.out.println("Invalid input!. You must enter either 'f' or 'b' to start.");
System.exit(1);
}
if (forwards) {
char c = 'a';
while (c < 'z') {
// checking if the scanned in character was the correct one
// if the it was not, then c will not be incremented, and it will loop again on the same letter.
if (scanner.next().charAt(0) == c) {
if (c == 'z') { // checking if it's the last letter, and if so, printing a special message
System.out.println("Correct! Well done!");
c++;
}
else { // otherwise, prompting the user to enter the next letter
System.out.println("[" + c + ": Correct! Now type '" + ++c + "']"); // c gets incremented
}
}
}
}
// method for going through the alphabet forwards
private static long forwards() {
// getting the time when the user started
long start = System.currentTimeMillis();
// looping for each of the 26 letters
int i = 0;
while (i < 26) {
// checking if the scanned in character was the correct one
// if the it was not, then i will not be incremented, and it will loop again on the same letter.
if (scanner.next().charAt(0) == alphabet[i]) {
if (i == 25) { // checking if it's the last letter, and if so, printing a special message
System.out.println("Correct! Well done!");
i++;
}
else { // otherwise, prompting the user to enter the next letter
System.out.println("[" + alphabet[i] + ": Correct! Now type '" + alphabet[++i] + "']"); // i gets incremented
}
}
}
// calculating the total time taken
long total = System.currentTimeMillis() - start;
// returning the total time taken in seconds (by dividing by 1000)
return total / 1000;
}
// method for going through the alphabet backwards
private static long backwards() {
// getting the time when the user started
long start = System.currentTimeMillis();
// looping for each of the 26 letters
int i = 25;
while (i >= 0) {
// checking if the scanned in character was the correct one
// if the it was not, then i will not be decremented, and it will loop again on the same letter.
if (scanner.next().charAt(0) == alphabet[i]) {
if (i == 0) { // checking if it's the last letter, and if so, printing a special message
System.out.println("Correct! Well done!");
i--;
}
else { // otherwise, prompting the user to enter the next letter
System.out.println("[" + alphabet[i] + ": Correct! Now type '" + alphabet[--i] + "']"); // i gets decremented
}
}
}
// calculating the total time taken
long total = System.currentTimeMillis() - start;
// returning the total time taken in seconds (by dividing by 1000)
return total / 1000;
}
}