#I need help with alot of things i am a begginger
66 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @pale badge! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.
this is not directly related with your question, but you can probably look at the SO post to get a general idea on how to use the scanner properly as you will most likely also run into this problem as well
First of all, nextLine is a method
so you need () after it
secondly, you are calling nextInt for reading an int and nextLine after it - so you are attempting to read two things
third of all, nextLine returns a string, so you logically cannot compare it with an integer
if you want the int, use option
yes
if (option == 1) { should work
you are calling nextInt() twice
every time you call nextInt(), it reads a number
try it
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.
every time you call nextInt(), it will read a new input
that would still call it twice
but you can
- call it only once
- save the result to a variable
- use the variable as often as necessary
yes
and I would also recommend using else if, yeah
works
you still call nextInt twice
try it
you will probably learn more when you try out stuff before asking others :)
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.
first get your curley brackets under control
the brackets define where a block starts and ends
so the first if only covers the single System.out.println
I think you want it like this
yes
and the second problem - every time you call nextInt, it will read a new number
if you don't call nextInt, it will not read a new number
but reuse the old one
if you want to read a new number, you need to call nextInt again
you can also change the existing variable
choice = input.nextInt();
without the int
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.
It's sort of mandatory syntax for Java programming.
The first line indicates that you're defining a class named HelloWorld. In Java, everything must be inside a class (or an instance type at least). Only said classes (or instance types) may be written not enclosed in anything
Second line indicates that you're declaring a method. public static void main(String[]) is a bit of a special case as only methods like that may serve as an entry point for a Java program
Instructions may only appear inside a method (or an instructions block at least, which is usually a method or a constructor)
Basically you can make a valid Java program if you make a sequence of instructions that you surround with these lines on top, and closing them on bottom
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Choose Your Own Story!");
System.out.println("A boy by the name of Sam once goes down the river all alone. It is a hot day and he wonders if he should go for a swim.");
System.out.println(" Choose what happens next: Option 1,: He goes swimming, Option 2,: He doesnt swim. ");
int option = input.nextInt();
if (option == 1 ){
System.out.println("When the boy goes swimming he goes diving to see if there are any animals. He thinks he is a good swimmer so he has no worries when he dives down, but then he sees a water snake. Choose what happens next: Option 1,: He still goes diving and looking for other animals, Option 2: He comes back up and stays away from the snake");
int selection = input.nextInt();
if (selection == 1){
System.out.println("haha");
}
if (selection == 2){
System.out.println("hahaha");
}
}
if (option == 2){
System.out.println ("Sam then goes goes back home to go eat some ice cream to cool off. As he goes back home he sees his friends playing outside. He wonders if he should go eat ice cream or go play with his friends. Choose what happens next: Option 1,: Go eat ice cream, Option 2,: Go play with friends ");
int choice = input.nextInt();
if (choice == 1){
System.out.print("haha");
}
if (choice == 2){
System.out.print("haha");
}
}
}
}
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Choose Your Own Story!");
System.out.println("A boy by the name of Sam once goes down the river all alone. It is a hot day and he wonders if he should go for a swim.");
System.out.println(" Choose what happens next: Option 1,: He goes swimming, Option 2,: He doesnt swim. ");
int option = input.nextInt();
if (option == 1 ){
System.out.println("When the boy goes swimming he goes diving to see if there are any animals. He thinks he is a good swimmer so he has no worries when he dives down, but then he sees a water snake. Choose what happens next: Option 1,: He still goes diving and looking for other animals, Option 2: He comes back up and stays away from the snake");
int selection = input.nextInt();
if (selection == 1){
System.out.println("Sam Continues to search for other animals under the river. he find all kinds of fish. He also became good friends with a fish and he decided to keep him. He heard some thunder and so he went home. When he went home he realised he had no meny to buy a tank for the fish. Choose what happens next,: Option 1: he frees the fish, Option 2: he puts the fish in a bowl for now and goes to find a job to get a tank");
}
if (selection == 2){
System.out.println(" Sam quickly came out the river and it was no longer sunny as clouds began to apear. He heard thunder and didnt know what to do. Choose what happens next : Opyion 1,: Continue to have fun and explore more things near the river, Option 2: Go home and find shelter before there is a possible storm.");
}
}
if (option == 2){
System.out.println ("Sam then goes goes back home to go eat some ice cream to cool off. As he goes back home he sees his friends playing outside. He wonders if he should go eat ice cream or go play with his friends. Choose what happens next,: Option 1,: Go eat ice cream, Option 2,: Go play with friends ");
int choice = input.nextInt();
if (choice == 1){
System.out.print("Sam goes inside his house where it was cool, and not hot. He sat on the couch for a little because he just came back from outside. Soon after he went to go get icecream but there was no more icecream left. He wondered what he should do. Choose what happens next,: Option 1: He goes walking to the nearest store in the sun, Option 2: He just stays home and relaxes.");
}
if (choice == 2){
System.out.print("Sam quickly goes to where his friends are playing as he too wants to play with them. They are playing soccer, a sport Sam doesnt like, but he is very good at it. He joins them and quickly dominates. His frinds start telling him that he should be a soccer player one day when he grows up, but Sam like football more and so he doesnt know what to do. Chose what happens next,: Option 1 Sam starts playing soccer more and starts pursuiting a soccer career, Option 2: Sam starts playing football more and starts pursuiting a football career");
}
}
}
}
if(...){...} else {...}
but you can also use else if
well the variable could change within the if
if you change it
yeah
do what you are able to do
maybe a bit more and learn the stuff you are yet unable to do
i dont think they are very good personally, i would not use them as a resource
for example. they start teaching these classes, which are data structures, before even explaining what data structures even are so you just end up thinking they are just another random class in the java standard library
not very beginner friendly