Java switch ⬇【4 minutes】
ฝัง
- เผยแพร่เมื่อ 7 ก.ย. 2024
- Java switch statement
#Java #switch #statement
public class Main {
public static void main(String[] args) {
// switch = statement that allows a variable to be tested for equality against a list of values
String day = "Friday";
switch(day) {
case "Sunday": System.out.println("It is Sunday!");
break;
case "Monday": System.out.println("It is Monday!");
break;
case "Tuesday": System.out.println("It is Tuesday!");
break;
case "Wednesday": System.out.println("It is Wednesday!");
break;
case "Thursday": System.out.println("It is Thursday!");
break;
case "Friday": System.out.println("It is Friday!");
break;
case "Saturday": System.out.println("It is Saturday!");
break;
default: System.out.println("That is not a day!");
}
}
}
public class Main {
public static void main(String[] args) {
// switch = statement that allows a variable to be tested for equality against a list of values
String day = "Friday";
switch(day) {
case "Sunday": System.out.println("It is Sunday!");
break;
case "Monday": System.out.println("It is Monday!");
break;
case "Tuesday": System.out.println("It is Tuesday!");
break;
case "Wednesday": System.out.println("It is Wednesday!");
break;
case "Thursday": System.out.println("It is Thursday!");
break;
case "Friday": System.out.println("It is Friday!");
break;
case "Saturday": System.out.println("It is Saturday!");
break;
default: System.out.println("That is not a day!");
}
}
}
Thank you for always taking the time to put the code in the comments.
@@_Anna_Nass_ he can easily copy and paste lol
@@_Anna_Nass_ because he is smart and helpful
You can write "->" instead of ":" and you don't need now writing break at the end
switch(day) {
case "Sunday" -> System.out.println("It is Sunday!");
case "Monday" -> System.out.println("It is Monday!");
or
switch(day) {
case "Sunday" -> {
System.out.println("It is Sunday!")
System.out.println("It's a free day!")
};
case "Monday" -> {
System.out.println("It is Monday!")
System.out.println("It's not a free day!")
};
}
if you need to run more than 1 command.
Sout enter and only bro code
System.out.println("Awesome explanation!") ;
No main method found in program
public class Human
{
public static void main(String args[])
{
System.out.println("Awesome Explanation!" );
}
}
@@Sqrt.Infinity incorrect indentation
quick reminder.
Now you can use lambda expression:
switch(number) {
1 -> do something;
2 -> do something;
3 -> do something
}
instead of
switch(number) {
case 1: do something;
break;
case 2: do something;
break;
case 3: do something
break;
}
what will be the default and break for this expression
It is not Lambda expression it is called (Lambda-like syntax) .. Lambda is the replication for the anonymous inner class ..but may be we can name as enhanced switch
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
You teaches the peaches, for real. Thank you so much for taking the time to create these lessons. You've designed them in such a order, that while I'm learning I can take a new concept that you're teaching me, and apply it with one of your previous lessons easily. Right now I'm creating switch statements while interfacing a GUI. I wish you would have squeezed a video about using getText() though, because Scanner can't be used with JOptionPane apparently x). Thank you for teaching me stuff and things, Proffeser Bro.
(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(๑˙❥˙๑(ʃƪ^3^)(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(๑˙❥˙๑)(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(๑˙❥˙๑)(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。(◍•ᴗ•◍)✧*。
Learning the beginnings of Java in school but the tricks in these tutorials are awesome like: sysout + strg/ctrl + space
An exciting and brief course by bro absolutely amazing may your channel grow big Af
your tutorials are always amazing thanks bro
Teachers seeing this and making it 2 hours long:
Tomorrow i've got a Java exam in school, and this helped a lot. Thanks
Maaaan you got 170 likes and 0 dislikes this is the proof that you are really really really good i think even a monkey would understand java with your lessons you are too good at explaining it
lol fr
For the default statement you should have said "That is not a day, but it should be :))."
Really love your videos.I need to support you!!!!
bro, thank you for everything, you helped me pass my ict subject and make coding fun!
Your the best Java Teacher on youtube.
best switch case explanation
im going to like all your videos. you are saving me. thx bro !!!
I always come back to this channel to learn anything
Love ur videos bro :>
Well explained!
Thank you so much. Well explained
You teached me alot sir, Thank you!
I don't know what i have to comment, just wanna comment to support this channel tho
THANK YOU homie. My ass had no idea what this was in class today. Saved my ass 30 minutes to rewatching lecture videos lol. I think my professor sucks.
Thanks man
very much appreciate it
the good thing in the switch tag is that we can put another condition inside of it.
BRO code !!! You are the chosen one, to be the None Indian TH-cam Teacher :))
I’ve been learning Java programming on my end for almost 1 month now, and all of the theoretical concepts and logic understanding is only on top of my head, I’ve got a MacBook, so I’m wondering if you have a recommendation about a program for coding to start with, I’d appreciate it since I tried but they did not work properly (
Thanks for your explanation Bro ! May I know why it shows default whenever I type Monday with small letter m. how can i solve to show correctly without caring Monday or monday
This is really lit bruh🔥🔥
So can you add 3 together like
Sunday , Monday and Tuesday
Instead of putting too many break ;
Thank you bro
Thank you, Bro Sensei👍
thank you brother you saved my day🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰😍😍😍😍😌😌😌😌
you are a hero bro
Thanks for the video
@Bro code While making a simple rock paper scissor program I had a recommendation for a rule switch in netbeans. What is a rule switch how and when should it be used?
I like your teaching style. hhhh
good video bro!
well explained
Can we apply switch case to integer also ?
Thanks, bro
i wonder if python has a similar way for handling multiple conditions
Great video!
Thank you again 💙💙
doing my part for the algorithm
Thank you🙌
One more thing you can do instead of having to change the day string value is to ask the user to input the day using scanner. However, my question here is how to implement equalsIgnoreCase here?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// switch = statement that allows a variable to be tested for equality against a list of values
Scanner scanner = new Scanner(System.in);
String day = scanner.nextLine();
switch(day) {
case "Sunday": System.out.println("It is Sunday!");
break;
case "Monday": System.out.println("It is Monday!");
break;
case "Tuesday": System.out.println("It is Tuesday!");
break;
case "Wednesday": System.out.println("It is Wednesday!");
break;
case "Thursday": System.out.println("It is Thursday!");
break;
case "Friday": System.out.println("It is Friday!");
break;
case "Saturday": System.out.println("It is Saturday!");
break;
default: System.out.println("That is not a day!");
}
}
}
Switching case the next lesson, default is learning
Yeah I become a hero !!
Easy! Thank you
What if i type "monday" instead of "Monday".
How can I ignore the case?
Btw thank u bro I love ur videos. Keep it up!
System.out.println("you gave one of the best explanation , thank you bro ");
A legend
you are amazing!
Super!
Thank you bro code!
I love you chanel !!!
Thanks
Thanks bro
thank you!
Ty bro
thanks
If I become president, I will make pizza the 8th day of the week.
How do I do it if I want to pick a random variable in the switch statement?
Ily so much
W EXPLANATION 💥
good to know
Thanks, bro. Algorithm comment
thankyou Bro
❤❤❤❤❤ great 🎉🎉🎉🎉
thx 4 vid bro
Thanku Bro🙏🙏🙏🙏
THank!
Thanks Bro code ❤
you are the best.
thank u so much
i though you need to give number for each case
thanks brah
nice
I did the way you taught. But when I run the program it says "Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted" whats wrong?
Me too, can you help me?
Look what you have to do
Right click on your project, go to Properties. Select Java Compiler from the left menu. Select your compliance level (1.7 or 1.6). 1.7 will stop that message. 1.6, as others said before, won't let you use strings
thank you @@mouradjaafar6013
legend
God Bless you Bro +7
thx
just switchted to the switch video
nOt FuNnY lOl
good video
thx learnt
Nice.
pizza is not a day indeed
could you make it so that it randomly generates a day from a list so that it says a random statement instead of you manually changing it?
of course you can!
@levimonarca did you create an array list and add all of the values into that? Sorry I’m just trying to understand
Hiw?
How?
Like it
doing well!
comment for stats. thanks!
4 february done
i dunno if i'll switch to eclipse after 2 years of netbeans
update did you switch?
in 3:37 seconds of your video why it is sunday! is not displayed
rest all days except monday and sunday are displayed while the switch is on monday only but else must have been
displayed but sunday is also in the output....kindly answer my doubt....
rest explaination were nice but i got a doubt in the above things that i have mentioned..
kindly clear my doubt bro......
pro gunga moment here
😊😊😊
cool!
comment for algorithm