#whenever i run my code it keeps saying "error: unreachable statement"

28 messages · Page 1 of 1 (latest)

grizzled rampart
#

import java.util.Scanner;

public class Main {
public static void main(String args[])
{

   Scanner sc = new Scanner(System.in);
   do {
        System.out.print("Would you like to know your Total Quarterly Salary? Press 1 if yes or Press 2 if no! ");
        int ch = sc.nextInt(); 
        switch (ch)
        {
        case 1: 
        System.out.println ("Enter Quarter's Salary of Region 1: ");
        int regiona = sc.nextInt();
        System.out.println ("Enter Quarter's Salary of Region 2: ");
        int regionb = sc.nextInt();
        System.out.println ("Enter Quarter's Salary of Region 3: ");
        int regionc = sc.nextInt();
        int kys = regiona + regionb + regionc;
        System.out.println ("Your Total Quarterly Salary is " +kys+ "!");

        if (kys > 5000) {
        System.out.println ("This year's Quarterly Sales are greater than the previous sales.");
        } 
        else {
        System.out.println ("                       ");
        System.out.println ("This year's Quarterly Sales are lesser than the previous sales.");
        }
        break;
   }
   }

   while(true);
    {
        int ch2 = sc.nextInt();
        switch (ch2) 
        {
        case 2:
        System.out.println ("                       ");
        System.out.println ("***********************");
        System.out.println ("This program has ended!");
        break;
        default:
        System.out.println ("                       ");
        System.out.println ("***********************");
        System.out.println ("This program has ended!");
        }
    }    
    
}

}

tight capeBOT
#

This post has been reserved for your question.

Hey @grizzled rampart! Please use /close or the Close Post button 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.

grizzled rampart
#

@fiery parrot hi sorry i need some help 😭

tight capeBOT
#
import java.util.Scanner;

public class Main {
    public static void main(String args[]) 
    {

       Scanner sc = new Scanner(System.in);
       do {
            System.out.print("Would you like to know your Total Quarterly Salary? Press 1 if yes or Press 2 if no! ");
            int ch = sc.nextInt(); 
            switch (ch)
            {
            case 1: 
            System.out.println ("Enter Quarter's Salary of Region 1: ");
            int regiona = sc.nextInt();
            System.out.println ("Enter Quarter's Salary of Region 2: ");
            int regionb = sc.nextInt();
            System.out.println ("Enter Quarter's Salary of Region 3: ");
            int regionc = sc.nextInt();
            int kys = regiona + regionb + regionc;
            System.out.println ("Your Total Quarterly Salary is " +kys+ "!");

            if (kys > 5000) {
            System.out.println ("This year's Quarterly Sales are greater than the previous sales.");
            } 
            else {
            System.out.println ("                       ");
            System.out.println ("This year's Quarterly Sales are lesser than the previous sales.");
            }
            break;
       }
       }

       while(true);
        {
            int ch2 = sc.nextInt();
            switch (ch2) 
            {
            case 2:
            System.out.println ("                       ");
            System.out.println ("***********************");
            System.out.println ("This program has ended!");
            break;
            default:
            System.out.println ("                       ");
            System.out.println ("***********************");
            System.out.println ("This program has ended!");
            }
        }    
        
    }
}
fiery parrot
#

you have a do-while loop

#

and that one never ends

#

the break; only breaks out of the switch

#

so everything after the while(true); can never be reached

grizzled rampart
#

ohh okok wait

#

soo what do i have to do to fix this?

rocky gulch
#

your switch always does the "ended" thing, so you don't even really need the loop

#

but seems like that's a separate mistake

#

you can label the loop to break out of the right statement:

loop:
while (...) {
  switch (x) {
    case "a": break loop;
  }
}
grizzled rampart
rocky gulch
#

i mean, you actually need the label first

#

the loop: is important

grizzled rampart
#

idk anymore😭

fiery parrot
#

you need to do it before the do{

grizzled rampart
#

so like this?

fiery parrot
#

yes

grizzled rampart
#

well i did that and it still said undefined label for loop

fiery parrot
#

Can you show your whole code again?

#

and please format it properly

grizzled rampart
#

public class Main {
    public static void main(String args[]) 
    {

       Scanner sc = new Scanner(System.in);
       loop:
       do {
           
            System.out.print("Would you like to know your Total Quarterly Salary? Press 1 if yes or Press 2 if no! ");
            int ch = sc.nextInt(); 
            switch (ch)
            {
            case 1: 
            System.out.println ("Enter Quarter's Salary of Region 1: ");
            int regiona = sc.nextInt();
            System.out.println ("Enter Quarter's Salary of Region 2: ");
            int regionb = sc.nextInt();
            System.out.println ("Enter Quarter's Salary of Region 3: ");
            int regionc = sc.nextInt();
            int kys = regiona + regionb + regionc;
            System.out.println ("Your Total Quarterly Salary is " +kys+ "!");

            if (kys > 5000) {
            System.out.println ("This year's Quarterly Sales are greater than the previous sales.");
            } 
            else {
            System.out.println ("                       ");
            System.out.println ("This year's Quarterly Sales are lesser than the previous sales.");
            }
            break;
       }
       }
       
       
       while(true);
       
       {
            int ch2 = sc.nextInt();
            switch (ch2) {
            case 2: break loop;
            System.out.println ("                       ");
            System.out.println ("**");
            System.out.println ("This program has ended!");
            break;
            default:
            System.out.println ("                       ");
            System.out.println ("**");
            System.out.println ("This program has ended!");
            }
        }

    }
}
rocky gulch
#

your switch isn't in the do while

#

you just have a stray block outside the loop

grizzled rampart
#

Ahh I see now, thank you so much you guyshidethepain