[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

switch statement in for loop

Hello I am trying to have a switch statement work inside of a for loop-
am attaching sample-
in the example - want user to be able to choose 1 or 2 -
can you see mistakes and what I am not doing to make this work?
thank you
testprogram.txt
0
davetough
Asked:
davetough
1 Solution
 
basav_comCommented:
Your Scanner statement should be inside main().
import java.util.Scanner;

public class Test_Program
{ 
    
    public static void main(String[] args) 
    {
        System.out.printf( "Java switch statement inside for loop" );
        
        //for loop      
        for ( int value = 1; value <= 10; value++ )
        {
            
            System.out.printf( "Enter 1 or 2: ");
            Scanner input = new Scanner(System.in);
            
            int choice = input.nextInt();
            
            switch (choice)
            {   
                case 1:
                System.out.println("Test 1");
                break;
                case 2:
                System.out.println("Test 2");
                break;      
            }
        
        }
    }
}

Open in new window

0
 
davetoughAuthor Commented:
thank you
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now