Compare your project to our solution code and share your project below! Your solution might not look exactly like ours, and that’s okay! The most important thing right now is to get your code working as it should (you can always refactor more later). There are multiple ways to complete these projects and you should exercise your creative abilities in doing so.
This is a safe space for you to ask questions about any sample solution code and share your work with others! Simply reply to this thread to get the conversation started. Feedback is a vital component in getting better with coding and all ability levels are welcome here, so don’t be shy!
About community guidelines: This is a supportive and kind community of people learning and developing their skills. All comments here are expected to keep to our community guidelines
How do I share my own solutions?
If you completed the project off-platform, you can upload your project to your own GitHub and share the public link on the relevant project topic.
If you completed the project in the Codecademy learning environment, use the share code link at the bottom of your code editor to create a gist, and then share that link here.
Do I really need to get set up on GitHub?
Yes! Both of these sharing methods require you to get set up on GitHub, and trust us, it’s worth your time. Here’s why:
Once you have your project in GitHub, you’ll be able to share proof of your work with potential employers, and link out to it on your CV.
It’s a great opportunity to get your feet wet using a development tool that tech workers use on the job, every day.
Not sure how to get started? We’ve got you covered - read this article for the easiest way to get set up on GitHub.
Best practices for asking questions about the sample solution
Be specific! Reference exact line numbers and syntax so others are able to identify the area of the code you have questions about.
First time using git and sharing it. Hopefully i did it correctly.
My Magic 8-ball project file from Codecademy.
recommendations for improvement are much appreciated.
Thanks to all who took time out of their day to see my code!
Only just started learning Java over the past few days, let me know how my program is:
I know that originally all of the responses could have been in the same ArrayList but I have kept them in seperate (positive, negative, vague) so if any need to be modified in the future it is easier to change them, however the code would be more efficient without having to combine the ArrayLists
Usage of Scanner is amazing! The comments are very helpful as well
You might want to consider @samnatale idea of using ArrayLists. Combining both your ideas, I would have the user input a mood (e.g. choose between happy, sad, indifferent) via scanner: this will determine which ArrayList will be used to generate the output. Just sharing some thoughts
I downloaded IntelliJ IDEA yesterday. One of the things I found out was that I only needed to type in ‘out.println()’ to print. I used a technique where I created a random double and converted it to an integer. I then used a switch statement for the magic 8 ball. I am open to comments but I am very new to coding so please go slow with me.
My solution to the Codecademy Magic-8-Ball Challenge Project.
I commented out the scanner part because I have not used it before and couldn’t figure out how to make it work. Any help with this would be greatly appreciated.
I ran your code on my terminal and it worked (almost) perfectly even with the scanner! Just need a few touches but the main functionality is there.
I just took the Line out of String question = scan.nextLine();… so
public class MagicEightBall {
public static void main(String args) {
System.out.println("Please ask your question!!!");
Scanner scan = new Scanner(System.in);
scan.nextLine();
scan.close();
Random rand = new Random();
int r = rand.nextInt(6);
if(r == 0) {
System.out.println("Totally think that's possible!!");
} else if(r ==1){
System.out.println("Great I love learning new things!!");
} else if(r == 2) {
System.out.println("Ordinarily I would agree with you!!");
} else if(r == 3) {
System.out.println("Lets just agree to disagree!!");
} else if(r == 4) {
System.out.println("Don't count on it!!");
} else if(r == 5) {
System.out.println("I do not understand the question!!");
}
}
I presume your return statement on line 31 is to guard against generated numbers that aren’t from 1 to 8 inclusive. However, another way to accomplish this is using default in your switch statement, which executes certain code when the argument passed to switch doesn’t match any of the cases. You can find more about the switch statement here.
Additionally, a minor thing is that you’ve capitalized your parameter, Num. Variable names in Java should use camelCase.
import java.util.Scanner;
public class Magic8 {
public static void main(String[] args) {
System.out.println("Welcome to Magic 8 Ball!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Press E to Exit or C to Continue.");
Scanner scanner = new Scanner(System.in);
char input = scanner.next().charAt(0);
Boolean isActive = true;
while (isActive) {
if (Character.toUpperCase(input) == 'E') {
System.out.println("Goodbye!");
scanner.close();
isActive = false;
return;
} else if (Character.toUpperCase(input) == 'C') {
System.out.println("What is your question? Type your question an press Enter.");
scanner.next();
isActive = false;
try {
System.out.print("The answer to your question...");
Thread.sleep(1000);
System.out.println(EightBall.Answer());
scanner.close();
} catch (InterruptedException e) {
e.printStackTrace();
scanner.close();
isActive = false;
}
} else {
System.out.println("Sorry, I did not understand what you typed in.");
System.out.println("Press E to Exit or C to Continue.");
input = scanner.next().charAt(0);
}
}
}
}
EightBall.java
import java.util.Random;
public class EightBall {
public static String Answer() {
Random random = new Random();
int randomNumber = random.nextInt(20);
String answer = "";
switch (randomNumber) {
case 0:
answer = "It is certain.";
break;
case 1:
answer = "It is decidedly so.";
break;
case 2:
answer = "Without a doubt.";
break;
case 3:
answer = "Yes - definitely.";
break;
case 4:
answer = "You may rely on it.";
break;
case 5:
answer = "As I see it, yes.";
break;
case 6:
answer = "Most likely.";
break;
case 7:
answer = "Outlook good.";
break;
case 8:
answer = "Yes.";
break;
case 9:
answer = "Signs point to yes.";
break;
case 10:
answer = "Reply hazy, try again.";
break;
case 11:
answer = "Ask again later.";
break;
case 12:
answer = "Better not tell you now.";
break;
case 13:
answer = "Cannot predict now.";
break;
case 14:
answer = "Concentrate and ask again.";
break;
case 15:
answer = "Don't count on it.";
break;
case 16:
answer = "My reply is no.";
break;
case 17:
answer = "My sources say no.";
break;
case 18:
answer = "Outlook not so good.";
break;
case 19:
answer = "Very doubtful.";
break;
default:
answer = "Out of bounds in default! How did you do to get here?";
break;
}
return answer;
}
}
Not exactly step for step, but its a function app that replaces the toy.