[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –

Java Programming Tutorial – 12 – Switch Statement
by
Tags:
Comments
23 responses to “Java Programming Tutorial – 12 – Switch Statement”
-
Added my own little touches to this code by making the case take in user input from the keyboard. Your tutorials are very helpful, I'm learning Java from C++ for my new transfer college. You're a life saver, man!
-
cant we give the case a range in java like we do i c++? like:
case 1 .. 10:
do something; -
hi may i ask how do i input again if the user is not writing the correct answer like this code.
import java.util.*;
class aaa
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter age 1-3:t");
int age = s.nextInt();
switch (age)
{
case 1:
System.out.print("your age is one");
break;
case 2:
System.out.print("your age is two");
break;
case 3:
System.out.print("your age is three");
break;
default:
System.out.print("INVALID");}
}
} -
Is it necessary to put a default statement? If i had already provided with case 3: and i know that the program's not going to crash. But do I need to put it always?
-
I'm happy that I started in C++. Tried to learn lots in it and then here I am, java coding is like c++ that's why it takes me just 1 view on video to learn it.
-
When i finish a tutorial video, i say "Let's watch another one." You express things pretty well in a short period of time. I wish you were my teacher.I would attend to ur all classes.
-
nice explanation and really fast type writing
-
Instead of writing "System.out.println" you can just write "sysout" and press CTRL + Space.
-
disliked because you spelled 'trouble' wrong
-
this guy doesn't know to use the shortcut key for system println..just type syso ctrl space !
-
These are GREAT tutorials, but after watching them for a few hours in the middle of the night it gets kind of… boring.
-
How can I add a condition in a switch statement? like we add conditions in if else statements. for example, if variable>0 then do this. how do I do that with a switch statement?
-
Just a little bit of what I've learned from you! Thank you very much and cya you later. Btw, you rock!
import java.util.Scanner;
public class main {
public static void main(String args[]) {
int numar1, numar2, rezultat;
rezultat = 0;
Scanner imput = new Scanner(System.in);
System.out.print("Introduceti primul numar: ");
numar1 = imput.nextInt();System.out.print("Introduceti al doilea numar: ");
numar2 = imput.nextInt();System.out.print("Introduceti semnul ecuatiei preferate: ");
switch (imput.next()) {
case "+":
if (numar1 == 5) {
rezultat = numar1 + numar2;
System.out.println(
"This is a simple IF inside a switch command. Good Job! Oh yea, the answer is: " + rezultat);
} else {
rezultat = numar1 + numar2;
System.out.println("Rezultatul ecuatiei este: " + rezultat);
}
break;
case "-":
rezultat = numar1 – numar2;
System.out.println("Rezultatul ecuatiei este: " + rezultat);
break;
case "*":
rezultat = numar1 * numar2;
System.out.println("Rezultatul ecuatiei este: " + rezultat);
break;
case "/":
rezultat = numar1 / numar2;
System.out.println("Rezultatul ecuatiei este: " + rezultat);
break;
default:
System.out.println("Ecuatii disponibile: + , – , * , / ");
break;
}imput.close();
} -
Is it possible to that with string?
for example, like a traffic light, because mine wont work
public static void main(String[] args) {
String light;
light = red;
switch (light) {
case red:
System.out.println("Stop");
break;case yellow:
System.out.println("Get ready");
break;case green:
System.out.println("Go");
break;default:
System.out.println("Traffic light don't work");}
}
}
-
Nice, you're really cool when you explaining the program 🙂 HAHAHAHHAHA
-
String age;
System.out.println("Tell me your age?");
Scanner Srij = new Scanner(System.in);
age = Srij.nextLine();
### switch (age){
case "ONE":
System.out.print("utu");
break;
case "TWO":
System.out.print("You can talk");
break;
case "THREE":
System.out.println("You ca be in trouble");
break;
default:
System.out.println("I dont how old you are");
break;PLz tell me the mistake at Switch line. 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 at apples.apples.main(apples.java:12) -
can't we use it with multiple variable ??? please reply
-
Can we use condition in Switch case like
switch(i){
case i>85 : System.out.println("A grade");
break;
case i>65 : System.out.println("b grade");
break;
case i>55 : System.out.println("c grade");
break;
case i>36: System.out.println("d grade");
break;
default : System.out.println("fail");}
-
how can we use operands like < or > in switch statements?
-
Well spelled kind sir, well speled.
-
Now two year olds can talk. You're one heck of a dude, man (I mean, thanks for the video, I understand it now 🙂 :))
-
I like how you say just deal with it with the spelling errors.
-
Thanks bro
Leave a Reply