Java Coding Stuffz

Messages
347
Reaction score
1,413
Points
485
Location
Georgia, United States of America
I'm coding a test in Java. This is just for fun, got bored, started coding, hit a rut, fixed said rut, now we are here.

Anyway I am just wondering what you guys think of my basic B-Level code. :D

MAIN CLASS
Code:
import java.util.ArrayList;
import java.util.Scanner;

public class Quiz {

    public static void main(String[] args) {
     
        //Commence scanner 
       Scanner scan = new Scanner(System.in);
       //Starting the quiz.
       System.out.println("Hello, I'm Quizita, your personal quiz bot.\nWhat's your name?");
       String name = scan.nextLine();  //Name for later use
       System.out.println("Oh, hello "+name+". Ready to start the quiz?");
       String start = scan.nextLine();
       
       //Staring quiz with text for user
        if (start.equalsIgnoreCase("YES")) {
            System.out.println("Good let's commence.");
        }  else {
            System.out.println("Okay, I'll take "+start+" as a yes.");
        }
       
        //Creating list to hold questions for test
        ArrayList<QAA> qaaL = new ArrayList<QAA>();
       
        //Adding questions | Use format below
        //qaaL.add(new QAA("Question blank text?"
        //        + "\nA) ExA\nB) ExB\nC) ExC", "<letter>"));
        qaaL.add(new QAA("What is the name of the Greek God of the Sea?"
                + "\nA) Zeus\nB) Poseidon\nC) Hades", "b"));
        qaaL.add(new QAA("What is the name of the father of the first Olympians?"
                + "\nA) Chronos\nB) Prometheus\nC) Titanus", "a"));
       
        ArrayList<String> ans = new ArrayList<String>();
       
        //Asking question, getting answer and logging
        for(int i = 0; i < qaaL.size(); i++) {
            System.out.println(qaaL.get(i).getQuestion());
            ans.add(scan.nextLine());
        } 
       
        //Calculating Score with Comparison 
        int score = 0;
        for(int i = 0; i < qaaL.size(); i++) {
            if(qaaL.get(i).getAnswer().equalsIgnoreCase(ans.get(i))) 
                score++;
        }
        //Ending result
        System.out.println("Your got "+score+" question(s) correct out of "+qaaL.size()+".");
    }
}

QUESTION AND ANSWER QAA CLASS
Code:
public class QAA {
private String question;
private String answer;

    public QAA(String question, String answer) {
        this.question=question;
        this.answer=answer;
    }
 
    public String getQuestion() {
        return this.question;
    }
 
    public String getAnswer() {
        return this.answer;
    }
}
 
Last edited:

Lee

Messages
17
Reaction score
29
Points
190
Location
Land of the Free home of the Brave
Many comments; good for starting out. When I contact I usualy space out the "+"s.

If you really want some basic challenges go ahead and install Karel J Robot: https://csis.pace.edu/~bergin/KarelJava2ed/karelexperimental.html. But based off what Im seeing you may not need it.

https://www.hackerrank.com/ is also a good little tool to find problems that match certain criterias. I ususally do one problem before I goto bed (makes me sleep well and not forget things).
 
Messages
700
Reaction score
5,370
Points
735
That's a pretty good job for a beginner!
If you want to make your life a lot easier I suggest using IntelliJ, literally the best IDE in existance.
Now if you want to have a challenge, make it pull the questions from database where you can easily add questions (it's a bit of a challenge but possible)
 
Messages
312
Reaction score
818
Points
500
Location
Xquality's basement
Woah nice job ;-;
You said beginner and I just felt the hello world text before I even opened it.

If you need anything feel free to ask away.

(Btw if you want to challenge yourself try binary reading and writing, not as hard but not as easy).
 
Messages
249
Reaction score
236
Points
390
Location
Texas
I can kinda sorta read Java, but can't write in it. I took the test btw. lol, I tried a site called codecadamy, and took like two lessons in Java because "minecraft uses it" lol.
 
Messages
6
Reaction score
12
Points
90
Location
Austria
Great first Java Programm. Although Scanner is good, I prefer to use BufferedReader, as this can read way faster then Scanner.
Here is a small code snippet on how to use it. You can ofcourse replace System.in with new File() to read from a file:

BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
String currentLine;

while ((currentLine = reader.readLine()) != null) {
//Read here
}
 
Top