Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Björn Norén
9,569 PointsSome pro tips please!
Hello there Treehouse community! I have made this Quiz-application where a user gets 5 questions asked, but I feel that my code are quite ugly, and I would really appreciate if someone could give me some tips on how to clean it up!
It's mostly in the "for-loop", it feels like I could re-use some code in a smart way
import java.util.*;
public class Quiz2
{
public static String[][] answersAndQuestions = new String[][]
{
{"1. How much is 1 + 1? \n- 1. 1\n- 1.5. x\n- 2. 2", "2"},
{"2. How much is 1 - 1? \n- 0. 1\n- 0.5. x\n- 1. 2", "1"},
{"3. How much is 1 * 1? \n- 0. 1\n- 1. x\n- 2. 2", "x"},
{"4. How much is 1 / 1? \n- 1. 1\n- 1.5. x\n- 2. 2", "1"},
{"5. How much is 1 % 1? \n- -1. 1\n- 0. x\n- 1. 2", "1"}
};
// *** VOIDS ***
public static void printLines(){
System.out.println("-------------------------");
}
// * Frågor
// *** VARIABLER ***
public static String prompt;
public static char answer;
public static int wrong = 5;
public static int points = 0;
public static void main (String[] args)
{
Scanner user_input = new Scanner( System.in );
for (int i=0; i<answersAndQuestions.length; i++)
{
System.out.println(answersAndQuestions[i][0]);
System.out.println("Answer:");
printLines();
prompt = user_input.next();
printLines();
prompt = prompt.toLowerCase();
answer = answersAndQuestions[i][1].charAt(0);
while (prompt.length() != 1)
{
System.out.println("You are only allowed to write 1 letter!");
printLines();
System.out.println(answersAndQuestions[i][0]);
System.out.println("Ditt svar:");
prompt = user_input.next();
}
char p = prompt.charAt(0);
while (p != '1' && p != 'x' && p != '2')
{
System.out.println("You can only write 1, x or 2!");
printLines();
System.out.println(answersAndQuestions[i][0]);
System.out.println("Answer:");
prompt = user_input.next();
p = prompt.charAt(0);
}
if (p == answer)
{
points++;
wrong--;
}
}
System.out.println("We have a result!!!");
System.out.println("You got " + points + " points and you answered the wrong answer " + wrong + " times.");
}
}
1 Answer
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsSo, Here is what I can say (Not Pro way):
If you were asked to write quiz program in NOT object-oriented languages like bash for example, you've done a great job. All is one one file, one method, no classes.
Java is however is a quintessence of Object-Oriented Languages. Which roughly saying means: dividing program into separate Classes each one of them should have its own logic. At least: Model-View-Controller Logic, where at least game logic- is separated from prompting logic.
Now what I see here is one class, and one very big main method, in which you put everything you knew.
If I just start telling how this program should be written using Object-Oriented principles, that will take me forever. And I don't need to, because here at Treehouse you have wonderful Java Track that learns how to code properly in Java using Object-Oriented Design.
So I suggest you save this code, join Java track, get until and finish "Local Development Environments" course, and after that come back to this very code, and refactor it. Sounds good :)?
Hope it does not sound offensive. Your code solves the problem, and your quiz probably works, but from point of view Java and its Object-Oriented Principles, this code is just not right.