Using java - using if else statement in a switch? - java

I'm pretty new to Java, and coding in general. In my class we need the user to input the month (1-12) and a day (1-30) and we determine on a set date (june 15 to sept 30th) if it is or is not monsoon season. I'm trying to use a if else statement in a switch to say that anytime before june 15 is not monsoon season but my code keeps showing both if and else statement. Any help is welcomed, thank you!
import java.util.*;
public class Monsoon
{ public static void main (String[]args)
{
Scanner kb = new Scanner (System.in);
// Prompt the user to enter month & Day
System.out.print("Please enter a month(1-12) and day (1-31): " );
int month = kb.nextInt();
int day = kb.nextInt();
// using a switch statement to show months
switch (month){
case 1:
System.out.println("is NOT monsoon season");
break;
case 2:
System.out.println("is NOT monsoon season");
break;
case 3:
System.out.println("is NOT monsoon season");
break;
case 4:
System.out.println("is NOT monsoon season");
break;
case 5:
System.out.println("is NOT monsoon season");
break;
case 6:
System.out.println("“is monsoon season");
// use if else statement so user knows that before june 15 is not monsoon season
if (day>=15)
System.out.print("it is monsoon season");
else
System.out.print("it is not monsoon season");
break;
case 7:
System.out.println("“is monsoon season");
break;
case 8:
System.out.println("“is monsoon season");
break;
case 9:
System.out.println("“is monsoon season");
break;
case 10:
System.out.println("is NOT monsoon season");
break;
case 11:
System.out.println("is NOT monsoon season");
break;
case 12:
System.out.println("is NOT monsoon season");
break;
default: System.out.println("not valid");
break;
}
}
}

Repetition is bad. You can do it like this, utilizing the fall-through feature of the switch-case:
boolean isMonsoon;
switch (month) {
case 7:
case 8:
case 9:
isMonsoon = true;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 10:
case 11:
case 12:
isMonsoon = false;
break;
case 6:
// use if else statement so user knows that before june 15 is not monsoon season
if (day >= 15)
isMonsoon = true;
else
isMonsoon = false;
break;
default:
System.out.println("not valid");
break;
}
if (isMonsoon)
System.out.println("is monsoon season");
else
System.out.println("is NOT monsoon season");
Or, since the monsoon season is a range, using comparison operators is probably more suitable than a switch-case:
if ((month >= 7 && month < 10) || (month == 6 && day >= 15))
System.out.println("is monsoon season");
else
System.out.println("is NOT monsoon season");

Related

About The switch Statement in Java

I'm still learning. Today I have a question in SoloLearn, into The switch Statement lesion:
int day = 3;
switch(day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
// Outputs "Wednesday"
and I thinks, I can have a example to remember about input and switch Statement: I want input a number, and 1 as Monday, 2 as Tuesday.... to 7 as Sunday, and repeat: 8 as Monday, 9 as Tuesday....
This is my code:
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner day = new Scanner(System.in);
if (day.nextInt()>7){
day.nextInt()=day.nextInt()%7;
}
switch(day.nextInt()){
case 1 :
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
case 7:
System.out.println("Sunday");
break;
}
}
}
and error. My knowledge's not enough to fix this thing (I think so, because i'm a newbie and still learning). Can you expand for me, fix this thing and thanks for teach!
Have fun!
The problem is here:
if (day.nextInt()>7){
day.nextInt()=day.nextInt()%7;
}
You're trying to assign a value back to the method call day.nextInt(), which you can't do. Instead, try something like the following:
int dayNum = day.nextInt() % 7;
Also, your switch includes a case for dayNum == 7, which will never be the case.
As mentioned by #kolosy You are missing a break statement below case 0 and by writing day.nextInt() four times, you are actually asking the user to enter four times.
Also you cant day.nextInt()=day.nextInt()%7; do this. By doing it you're trying to assign a value back to the method call Change your code of main function to this
Scanner day = new Scanner(System.in);
int enteredDay = day.nextInt();
if (enteredDay > 7) {
enteredDay = enteredDay % 7;
}
switch (enteredDay) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
break;
case 7:
System.out.println("Sunday");
break;
}
Add break after case 0:
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
break;
If the value in switch() goes beyond 7 than you will get error as you have not provided any default case here. You can either do something like number%7to get the value always in case range.
You are missing a break statement below case 0. Java does not allow label fall-through like some other languages.
Also - you should be checking for your number being >= 7, not >7, or you'll never get a 0 on the second week. 8 % 7 is 1
NextInt() is a method .. you can't assign it a value
You need to use nextInt() once to get the input and store it in a variable. Every time you call nextInt() it would look for the next integer value entered.
Scanner day = new Scanner(System.in);
int dayInput = day.nextInt();
if (dayInput>7){
dayInput=dayInput%7;
}
switch(dayInput){
case 1 :
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
case 7:
System.out.println("Sunday");
break;
}
You could solve this by creating an int value to set the input for day as day.nextInt() then you can do the math in the created variable and make the switch depending on that value, this should work:
Scanner day = new Scanner(System.in);
int dayVal = day.nextInt();
if (dayVal>7){
dayVal %= 7;
}
switch(dayVal){
case 1 :
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 0:
System.out.println("Sunday");
break;
}
}
As said before, you need to change 7 to 0 because you will never have a residue of 0, therefore you will never get a Sunday

Switch Statement and Case

I am making code for my CPSC class and I have to print up to a case number that is set for the int. When I enter "2", the code prints "two potato" eight times instead of "One potato, two potato".
Here is my code I have:
public class Potato {
public Potato() {
}
public void count(int c) {
for (int i = 0; i < 8; i++) {
switch (c % 8) {
case 1: System.out.println("One potato"); break;
case 2: System.out.println("two potato"); break;
case 3: System.out.println("three potato"); break;
case 4: System.out.println("four..."); break;
case 5: System.out.println("five potato"); break;
case 6: System.out.println("six potato"); break;
case 7: System.out.println("seven potato"); break;
case 8: System.out.println("more!"); break;
default: break;
}
}
}
}
I think my problem is my for-loop, not too sure though, since I am here asking for help. Thanks in advance!
It is because c % 8 (2 % 8 = 2), so in every loop it will execute case 2 and prints two potato. You could use i % 8 instead.
This should do the trick. As others have said, you should use i%8. However, for this to work you should also start with i=1 since 0%8 = 0. And your case 8 should be changed to case 0.
for (int i = 1; i <= c; i++) {
switch (i % 8) {
case 1: System.out.println("One potato"); break;
case 2: System.out.println("two potato"); break;
case 3: System.out.println("three potato"); break;
case 4: System.out.println("four..."); break;
case 5: System.out.println("five potato"); break;
case 6: System.out.println("six potato"); break;
case 7: System.out.println("seven potato"); break;
case 0: System.out.println("more!"); break;
default: break;
}
}
This code is... peculiar:
The main problem is that you are not using the i variable that is incremented at the for loop. Instead, you are using c variable:
switch (c % 8)
Should be:
switch (i % 8)
You don't need the case 8 since there is no way that i % 8 result in 8. And, looks like you don't need c variable at all.

How can I get this switch to pick just one of the 3 options?

System.out.println("Please make a selection between 1 and 3");
int choice = s.nextInt();
switch (choice)
{
case 3: System.out.println("Can a brother get some fries with that?");
case 2: System.out.println("Cheeseburger it is.... fatso");
case 1: System.out.println("Good choice, you could use a salad");
break;
default: System.out.println("Not a valid selection bruh");
}
switch (choice) {
case 3: System.out.println("Can a brother get some fries with that?"); break;
case 2: System.out.println("Cheeseburger it is.... fatso"); break;
case 1: System.out.println("Good choice, you could use a salad"); break;
default: System.out.println("Not a valid selection bruh"); break;
}
I literally just added the breaks after each case so it will exit your switch if it encounters either 1,2,3 or none of them.
You are missing the break statements...
You need a break; after each of the cases.
switch (choice)
{
case 3: System.out.println("Can a brother get some fries with that?");
break;
case 2: System.out.println("Cheeseburger it is.... fatso");
break;
case 1: System.out.println("Good choice, you could use a salad");
break;
default: System.out.println("Not a valid selection bruh");
break;
}

I'm having an "Error: Syntax error on token(s), misplaced construct(s)". Where is my error?

import java.util.Scanner;
public class LetterGrade
{
public static void main(String[] args)
{
char grade;
String input;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter grade awarded");
input = keyboard.nextLine();
grade = input.charAt(0);
switch (grade)
{
case 'A':
System.out.println("Grade was between " + 89.5 - 100);
break;
case 'B':
System.out.println("Grade was between " + 79.5 - 89.45);
break;
case 'C':
System.out.println("Grade was between " + 69.5 - 79.49);
break;
case 'D':
System.out.println("Grade was between " + 59.5 - 69.49);
break;
case 'F':
System.out.println("Grade was below " + 59.5);
break;
System.out.println("Invalid grade inputted ");
}
}
}
4 errors found:
File: C:\Users\Raj\Downloads\Java\LetterGrade.java [line: 19]
Error: The operator - is undefined for the argument type(s) java.lang.String, int
File: C:\Users\Raj\Downloads\Java\LetterGrade.java [line: 22]
Error: The operator - is undefined for the argument type(s) java.lang.String, double
File: C:\Users\Raj\Downloads\Java\LetterGrade.java [line: 25]
Error: The operator - is undefined for the argument type(s) java.lang.String, double
I fixed the big errors i had, sorry about that. I really wasn't thinking straight and I understand i need to read more, which i will be doing tonight, but I'm have a different error now.
Problems:
1. grade = keyboard.next.Int(); should be grade = keyboard.nextInt();
2. Case A;
a. Case should be case
b. What is A you switch an int but there is no variable A that
holds any int value. Also if you mean the char A the you should
change it to 'A'
You need a : instead of ;after case. So something like case 'A':
3. switch (grade); remove the ;. Change it to switch (grade)
4. Not sure about this Any other character; Hopefully its meant to be a comment
First Issue:
grade = keyboard.next.Int();
change to
grade = keyboard.nextInt();
Second Issue:
your The switch Statement is clearly wrong
for example
switch (grade); <--- wrong
{
Case A; <--- wrong
your grade is type int but your case has type char
Case in wrong, you should have case
Look at this sample as your blue print
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}
Source of Code and Read more about Switch Statement
There should not be a semi-colon at the end of line 13. (The "switch" line).
Once you have fixed that, you will then see that the syntax of the "case" clauses is wrong.
And "any other character" is nonsense from the Java perspective. (I'm surprised that the Java compiler didn't throw a house-brick at you!)
I'm not sure what's wrong with what I'm doing.
You are trying to write Java without bothering to learn it properly. That is wrong on many levels.
I recommend that you read the Oracle Java Tutorial. For this example, read the Switch Statement page.

Need help in understanding this switch code - Days of the months

I am learning Java at the moment and I have some problems understanding the use of switch here. I would really appreciate it if someone could explain it simple to me.
So here's the code:
public class SwitchDemo2 {
public static void main(String[] args) {
int month=3;
int year=2000;
int NumDays=0;
switch(month) {
case 1: case 3: case 5: case 7: case 8: case 10:
case 12: NumDays=31; break;
case 4: case 6: case 9: case 11: NumDays=30;
break;
case 2: if ( ( (year%4 == 0) && !(year%100==0) ) ||
(year%400==0))
NumDays=29;
else NumDays=28;
break;
default: System.out.println("Invalid month.");
break;}
System.out.println("Number of Days= "+NumDays);
}}
What I don't understand is the definiton after case 2. "year%4==0" - alright, that makes sense, that's a leap year, but what about !(year%100==0) or (year%400==0) ? Why do we use that?
Thanks in advance!
By the definition of leap year:
if year is divisible by 400 then is_leap_year
else if year is divisible by 100 then not_leap_year
else if year is divisible by 4 then is_leap_year
else not_leap_year

Categories

Resources