Help on a Java lab for school
Hello! I would like help on a Java lab for school, I'm really confused on it :((
61 Replies
⌛ This post has been reserved for your question.
Hey @soybean 𝜗𝜚! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
What exactly is the issue/What confuses you?
im new to coding so idk the vocab much bc i was absent for sometime so idk how to answer these questions and i need to do my own experiment for this lab

Okay let's go over this step by step
TY MUCHH
I did some of it would u like to see
but my experiment for it does not work

1) A variable's datatype is defined in it's declaration. Declaration means basically definition, so the first time the variable is written down in code:
<datatype> <variablename>; or <datatype> <variablename> = <something>; (in the second variant we also initialize the variable. That means we give it its first value)
2) The Scanner is a class from the Java API. Just google "java se25" or "java se24", depends on the version you are using, and then click on the "overview" site (https://docs.oracle.com/en/java/javase/24/docs/api/index.html). There you can enter the name of your class (Scanner) in ther searchbar, find it and then read up on what it does
3) Those questions can be answered by reading about those methods in the same source as in "2)"
4 & 5) You basically have two types of so called control (flow) structures in imperative programming (like Java, Python, C, C++, C#...). For one you have decision statements, like if & else if & else or switch & case. With these 2 thingys you can choose the only execute one "path" in your code, you make a choice in an either do this or do that sense. If the if-condition is met you only execute the following block (in the {} and nothing else). Same thing goes for else if and the cases in switch (though their code blocks are not in {} but in : ... break;) . Now if no if/else if or case is true, the else or default block is executed (it has no condition). Example:
alternatively with switch case:
Now as second type of control structuers you have loops. Loops allow you to execute a block of statements. There are also two (and a half) types for that. while-loops allows you to repeat the block as long as the condition in the () of the while is true. This is checked again and again after each iteration. The for-loop is very similar to that, but it allows you to iterate with an index, so basically for a certain amount of times over the block. Example:
Or in for:
Lastly, what did i mean by 2 and a half control structures for loops? There are also so called do-while-loops. How do they work? Example:
You wanna do your experiment with x wit if-else if-else, not with 3 ifs btwoh ok
What doesn't work about it?^^
it doesn't print "Hello Mr.Taylor!"
into the console
Enter 3 and send a screenshot, what happens
okii

i didnt enter 3 bc it wont run bc of the error
you tried to use the variable
z which you didn't set to any value
initializing = setting a variable to a valueBut z was never used, or am i stupid?
At least from what i can see here
OHHHHHHH
i needed to create my own variable and set it to value
Can you like show the code again pls
so i can show him i tried experimenting to make the program run smth new
sureee
Without the overlaying error msg

Yeah, you never read a value into z
So how should the 3 you input end up in z?
ohhh ok i see now loll one sec
You could for one change the first two ifs to
Cleaner code.
YEAH OK ILL DO THAT RN
AND SEE

like this?
in the last if
you got z
instead of x
if z == 3
oh ty for pointing that out lemme fix it

You cooked the syntax xD
If you wanna do it like that, you have to remove the "old" ifs
ohhhhh ok
one sec
tyyyy
im so stupid, but what r the "old ifs"
For example, why is the
else if (x == 2) { followed by another if (x == 2) {?
And also, speaking about the syntax, why is there just an opening { before the if (x == 3) {, why is this if even still here?
No offence, but do you understand, what you are doing here?not really, im like really really new to coding lol
Read this again^^
If it doesn't help tell me^^
okay!
Also, do you know what curley brackets do in Java/C/C++ etc.
no, wait tbh i was absent for the first 3 to 4 blockdays of this course
so not rlly
{ } defines a scope. A scope can be a class ,like HelloWorldChoice, or a method, like the main or a control flow structure, like if, while, for, switch, else if, else
In these scopes you can do stuff (gross oversimplification, but enough for now)
Now, check your code again, anything wrong with the scopes?
Hint: you open a scope with { and close it with }. You also ideally use a TAB to indents between opening and closing {}, so you have the scopes nicely formatted
No need to think about the functionallity of classes, methods or control flow structures! Just tell me if the scopes look like they shouldohhh yeah i removed some of the scopes
What do you mean by that
the extra ones, the errors told me to remove some of them
Yeah that fixed it, but what was the origin of that mistake in the first place? To be more direct, what did those extra brackets fuck up?
the syntax? is that what ur asking?
Yeah that also, but basically you destroyed the hierarchy of the scopes
Like how they are stacked into each other
The main-scope is inside of the HelloWorldChoice-scope. The if-, else- and else if-scope is in the main-scope. You added some if-scopes inside of the else if-scope but forgot the closing } for that, boom code kaputt.
Was that explanation understandable
This thingy for example

The if doesn't get a proper closing }
Or the strange opening { in front of the
if (x==3). This opens some scope, but it has no key word in front of it, so it's absolutely illegal xDOmg thank so much for helping, ur a life saver loll
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
No problem
:))
So, how does the fixed code look like?
omg i didnt see ur text 😭

😱
should look like this
the line with dashes is where u put system out println
try this
okay!
Why are there 2 opening brackets? Why is there nothing happening in the if? Why are the last two System.out.println in the same case, the else if-case?

one sec
loll
Luls😂
oh imma switch to doing chem but ill come back to this and ill show it to u guys
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.