I've been tasked with creating a program using a switch statement to print the month names according to the exact number of days in the month. The program also needs to display an "error" message if the number of days entered does not correspond to any month. My code achieves these two goals. However i am wondering if there is shorter way to get the "error" message using a "less than" statement for when the user enters a number less than 28
import java.util.Scanner;
public class months {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int numb_days = 0;
System.out.println("Enter the # of days");
numb_days = keyboard.nextInt();
//This program will print 'May' when you run it.
String monthName;
switch (numb_days) {
case 30: monthName = "April, June, September, November ";
break;
case 31: monthName = "January, March, May, July, August, November, October, December ";
break;
case 28: monthName = "February";
break;
case 29: monthName = "February (only on leap years)";
break;
case 1: monthName = "Unknown";
break;
case 2: monthName = "Unknown";
break;
case 3: monthName = "Unknown";
break;
case 4: monthName = "Unknown";
break;
case 5: monthName = "Unknown";
break;
case 6: monthName = "Unknown";
break;
case 7: monthName = "Unknown";
break;
case 8: monthName = "Unknown";
break;
case 9: monthName = "Unknown";
break;
case 10: monthName = "Unknown";
break;
case 11: monthName = "Unknown";
break;
case 12: monthName = "Unknown";
break;
case 13: monthName = "Unknown";
break;
case 14: monthName = "Unknown";
break;
case 15: monthName = "Unknown";
break;
case 16: monthName = "Unknown";
break;
case 17: monthName = "Unknown";
break;
case 18: monthName = "Unknown";
break;
case 19: monthName = "Unknown";
break;
case 20: monthName = "Unknown";
break;
case 21: monthName = "Unknown";
break;
case 22: monthName = "Unknown";
break;
case 23: monthName = "Unknown";
break;
case 24: monthName = "Unknown";
break;
case 25: monthName = "Unknown";
break;
case 26: monthName = "Unknown";
break;
case 27: monthName = "Unknown";
break;
default: monthName = "Unknown";
break;
}
System.out.println(monthName);
}
}
The default statement is sufficient for handling all other cases that aren't caught by the specific cases for 28-31. So you can remove all other cases 1-27.
If you are required to use a less than statement you can use it before using the switch statement:
if (numb_days < 28) {
month_name = "unknown";
} else {
// Switch statement goes here
}
Related
Problem: Ask the user to enter 0-9999 and convert it into words using switch case and JOptionPane...
My question: I figure out how to convert the other numbers but if I entered 11-19, the output for example I enter 11 - "Ten One" how can I get the right output for this?
public class NumbertoWords {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int num=Integer.parseInt(JOptionPane.showInputDialog("Enter a Number from [0-9999]"));
int thou=num/1000;
int temp=num%1000; // 9999
int hun=temp/100; //999 -> 9
int temp1=temp%100;
int tens=temp1/10; //99
int ones=temp%10;
String display="";
switch(thou){
case 9: display=" Nine Thousand"; break;
case 8: display=" Eight Thousand"; break;
}
switch (hun){
case 9: display=display+ " Nine Hundred"; break;
case 8: display=display+ " Eight Hundred"; break;
}
switch (tens){
case 9: display=display+ " Ninety"; break;
}
switch (ones){
case 9: display=display+ " Nine"; break;
}
JOptionPane.showMessageDialog(null, display,"Number to Words",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
I would like to use an statement for multiple cases and then an extra statement for a single case.
switch (i)
{
case 1:
case 2:
statement1;
break;
case 1:
statement2;
break;
}
So for case 1 statement1 and statement2 should be executed.
You could delete the second case 1 in your code and move statement2 to your first case 1. But this only works if the order of the statements is not important (i.e. statement2 can be executed before statement1):
switch (i)
{
case 1:
statement2;
//Fall through
case 2:
statement1;
break;
default:
pleaseDoNotForgetMe();
}
If the order is important, I think you have no choice but give each case its break:
switch (i)
{
case 1:
statement1;
statement2;
break;
case 2:
statement1;
break;
default:
pleaseDoNotForgetMe();
}
What is wrong whith this code? I have tested it many times but something still doesn't work.
var playernow = 1; //Whose player's turn it is (1 or 2)
function bobenli(playernow){
var wert="";
switch (playernow){
case "1":
wert="X";
playernow=2;
case "2":
wert="O";
playernow=1;
}
alert(playernow);
}
No break statement, both cases get executed. Also as others have said you are comparing ints to strings. You can drop the quotes in the cases.
var playernow = 1; //Welcher Spieler dran ist )(1 oder 2)
function bobenli(playernow){
var wert="";
switch (playernow){
case 1:
wert="X";
playernow=2;
break;
case 2:
wert="O";
playernow=1;
break;
}
alert(playernow);
}
var playernow = 1; //Welcher Spieler dran ist )(1 oder 2)
function bobenli(playernow){
var wert="";
switch (playernow){
case "1":
wert="X";
playernow=2;
break;
case "2":
wert="O";
playernow=1;
break;
}
alert(playernow);
}
you missed break statement in following switch case... http://jsfiddle.net/yjusC/
You need a break at the end of each case:
switch (playernow){
case "1":
wert="X";
playernow=2;
break;
case "2":
wert="O";
break;
First: no break, second: you're comparing different types (int and string). This might have no impact now, but you should be careful in general.
case "1": // This is looking for a string. You set playernow equal to an integer value above.
I am righting a program to help me track my mileage for work. I only travel to specific locations. I made a switch that has all of the possibly start and end locations I could have, along with their distance. right now when I run my program, a window pops up asking were I went for every location. I have to type the end location as the next start location every time. I want to be able to type out one long string (ex: location1>location2+location3>location4) and have it calculate the total mileage.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Main extends JFrame {
public static void main(String[] agrs) {
double total = 0;
double d = 0;
int c = 0;
int t = 0;
//Scanner input = new Scanner(System.in);
// Scanner input1 = new Scanner(System.in);
// String place;
JOptionPane.showMessageDialog(null, "MILEAGE CALCULATOR \n\nLocations: ADMIN, AE, BE, CE, CHS, CJHS, IE, NSE, PKE, RHS, SMS, SPE \nFormat: Location1>Location2");
// JOptionPane.showMessagedialog(null,"Locations: ADMIN, AE, BE, CE, CHS, CJHS, IE, NSE, PKE, RHS, SMS, SPE");
//System.out.println("MILAGE CALCULATOR \nLocations: ADMIN, AE, BE, CE, CHS, CJHS, IE, NSE, PKE, RHS, SMS, SPE \nFormat: LOCATION1>LOCATION2");
t = Integer.parseInt(JOptionPane.showInputDialog(null, "How many places did you go to from the admin building?"));
//t = input.nextInt();
//t++;
// System.out.println(t);
// System.out.println(c);
while (t > c) {
String place = JOptionPane.showInputDialog(null, "where did you go?");
//String place = input.nextLine();
// place.equalsIgnoreCase(place);
d=0;
switch (place.toUpperCase()) {
case "ADMIN>AE":
case "AE>ADMIN":
d = 7;
break;
case "ADMIN>BE":
case "BE>ADMIN":
d = 2.8;
break;
case "ADMIN>CE":
case "CE>ADMIN":
d = 1.5;
break;
case "ADMIN>CHS":
case "CHS>ADMIN":
d = .6;
break;
case "ADMIN>CJHS":
case "CJHS>ADMIN":
d = 1.5;
break;
case "ADMIN>IE":
case "IE>ADMIN":
d = 2.3;
break;
case "ADMIN>NSE":
case "NSE>ADMIN":
d = 4.5;
break;
case "ADMIN>PKE":
case "PKE>ADMIN":
d = 2.6;
break;
case "ADMIN>RHS":
case "RHS>ADMIN":
d = 1.5;
break;
case "ADMIN>SMS":
case "SMS>ADMIN":
d = 3.9;
break;
case "ADMIN>SPE":
case "SPE>ADMIN":
d = 3.2;
break;
case "AE>BE":
case "BE>AE":
d = 9.5;
break;
case "AE>CE":
case "CE>AE":
d = 6.3;
break;
case "AE>CHS":
case "CHS>AE":
d = 7.6;
break;
case "AE>CJHS":
case "CJHS>AE":
d = 6.3;
break;
case "AE>IE":
case "IE>AE":
d = 6.3;
break;
case "AE>NSE":
case "NSE>AE":
d = 8;
break;
case "AE>PKE":
case "PKE>AE":
d = 7.6;
break;
case "AE>RHS":
case "RHS>AE":
d = 6.3;
break;
case "AE>SMS":
case "SMS>AE":
d = 8.2;
break;
case "AE>SPE":
case "SPE>AE":
d = 7.5;
break;
case "BE>CE":
case "CE>BE":
d = 3.7;
break;
case "BE>CHS":
case "CHS>BE":
d = 2.7;
break;
case "BE>CJHS":
case "CJHS>BE":
d = 3.7;
break;
case"BE>IE":
case "IE>BE":
d = 5;
break;
case "BE>NSE":
case "NSE>BE":
d = 4.2;
break;
case "BE>PKE":
case "PKE>BE":
d = 2.8;
break;
case "BE>RHS":
case "RHS>BE":
d = 3.7;
break;
case "BE>SMS":
case "SMS>BE":
d = 4.2;
break;
case "BE>SPE":
case "SPE>BE":
d = 7.4;
break;
case "CE>CHS":
case "CHS>CE":
d = 1.9;
break;
case "CE>CJHS":
case "CJHS>CE":
d = 0;
break;
case "CE>IE":
case "IE>CE":
d = 2.5;
break;
case "CE>NSE":
case "NSE>CE":
d = 3.5;
break;
case "CE>PKE":
case "PKE>CE":
d = 2.3;
break;
case "CE>RHS":
case "RHS>CE":
d = 0;
break;
case "CE>SMS":
case "SMS>CE":
d = 3.7;
break;
case "CE>SPE":
case "SPE>CE":
d = 3.3;
break;
case "CHS>CJHS":
case "CJHS>CHS":
d = 1.8;
break;
case "CHS>IE":
case "IE>CHS":
d = 2.9;
break;
case "CHS>NSE":
case "NSE>CHS":
d = 3.2;
break;
case "CHS>PKE":
case "PKE>CHS":
d = 2;
break;
case "CHS>RHS":
case "RHS>CHS":
d = 1.9;
break;
case "CHS>SMS":
case "SMS>CHS":
d = 3.3;
break;
case "CHS>SPE":
case "SPE>CHS":
d = 3.8;
break;
case "CJHS>IE":
case "IE>CJHS":
d = 2.5;
break;
case "CJHS>NSE":
case "NSE>CJHS":
d = 4.3;
break;
case "CJHS>PKE":
case "PKE>CJHS":
d = 2.3;
break;
case "CJHS>RHS":
case "RHS>CJHS":
d = 0;
break;
case "CJHS>SMS":
case "SMS>CJHS":
d = 3.7;
break;
case "CJHS>SPE":
case "SPE>CJHS":
d = 3.3;
break;
case "IE>NSE":
case "NSE>IE":
d = 5.8;
break;
case "IE>PKE":
case "PKE>IE":
d = 4.6;
break;
case "IE>RHS":
case "RHS>IE":
d = 2.5;
break;
case "IE>SMS":
case "SMS>IE":
d = 6;
break;
case "IE>SPE":
case "SPE>IE":
d = 3.1;
break;
case "NSE>PKE":
case "PKE>NSE":
d = 1.3;
break;
case "NSE>RHS":
case "RHS>NSE":
d = 3.5;
break;
case "NSE>SMS":
case "SMS>NSE":
d = .2;
break;
case "NSE>SPE":
case "SPE>NSE":
d = 7.2;
break;
case "PKE>RHS":
case "RHS>PKE":
d = 2.2;
break;
case "PKE>SMS":
case "SMS>PKE":
d = 1.5;
break;
case "PKE>SPE":
case "SPE>PKE":
d = 5.5;
break;
case "RHS>SMS":
case "SMS>RHS":
d = 3.7;
break;
case "RHS>SPE":
case "SPE>RHS":
d = 3.3;
break;
case "SMS>SPE":
case "SPE>SMS":
d = 7.4;
break;
}
// System.out.println(total);
total = d + total;
// System.out.println(total);
// JOptionPane.showMessageDialog("miles Driven ", d+total);
c++;
}
JOptionPane.showMessageDialog(null, "Miles Driven: " + total);
//System.out.println(total);
}
}
You could do something like:
int total = 0;
String[] places = place.split(">"); //Split your input string place into a list
for(int i = 0; i < places.length - 1; i++){
String leg = places[i] + ">" + places[i+1]; //Create string representing this leg of the journey
//Figure out the length of this leg of the journey using your switch statement
total += d; //d is the variable you were using in the switch
}
So if you entered "AE>Admin>BE>CJHS" the first iteration of the loop would set case to AE>Admin, and use your switch to set distance to 7 and total to 7, second time through should get Admin>BE, distance = 2.8 and total = 9.8, third iteration BE>CJHS, distance = 3.7 and total = 13.5 if I'm doing my mental math correctly. Then i would be incremented to 3, and since the length of the array is only 4, it would kick out of the loop.
If you wish to format your string differently, here's the java documentation for the string class and it's split method. http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)
Well it might be a dumb question, but I'm unable to find an answer:
The case is simple, I have a function with an integer in entry and two variable to assign depending of that. The problem is that the value assigned to the variables is common to certain case, but those case are not the same for the two variables. (if it's not clear enough, see the example).
I was wondering what was the best practice for such a case.
Is it something like :
function test(a){
var x,y;
switch (a){
case 0:
case 1:
case 7:
y=...;
break;
case 2:
case 6:
y=...;
break;
case 3:
case 4:
case 5:
y=...;
}
switch(a){
case 5:
case 6:
case 7:
x=...;
break;
case 0:
case 4:
x=...;
break;
case 1:
case 2:
case 3:
x=...;
}
...
}
or
function test(a){
var x,y;
switch (a){
case 0:
x=...;
y=...;
break;
case 1:
x=...;
y=...;
break;
case 2:
x=...;
y=...;
break;
case 3:
x=...;
y=...;
break;
case 4:
x=...;
y=...;
break;
case 5:
x=...;
y=...;
break;
case 6:
x=...;
y=...;
break;
case 7:
x=...;
y=...;
}
...
}
Or to use a mix of the two with the value assigned to x in each case and to make groups for the value of y?
Note that there might be more than 8 value, it's just for the example.
Thanks in advance.
If there's no real overlap between the different cases in the switch statements, you're probably better off having them separate.
If there was a majority of commonality between the actions, you could combine them with certain special cases within the individual case blocks but you seem to indicate that's not the case here.
However, if this isn't just a simple example of a much more complex case, you can achieve a more compact solution with something like:
// index: 0 1 2 3 4 5 6 7 8 9 10 11 12
static const int lookupX[] = { 2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9};
static const int lookupY[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9};
if ((a >=0) && (a < sizeof(lookupX) / sizeof(*lookupX)))
x = lookupX[a];
if ((a >=0) && (a < sizeof(lookupY) / sizeof(*lookupY)))
y = lookupY[a];
This will allow you to keep the values in a much smaller "configuration" section so that you can easily see the intent. The range checking and lookup then become very simple.
That code is tailored to C - I'm not sure what specific language you're using (because of the var statement) but it's basically only doing the lookup if the index will be valid. You'll need to translate that bit to your language of choice.