Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

switch case

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// from devaka's Practice Exam3 modified a bit

OUTPUT : Default Only A Only B Only C

It is taking int values declared in main. But I want to use char A ,char B declared as instance variables. Means I want Output as Only A Only B Only C . How can I use Char instance variables in switch case???
 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Banu,

If you convert (char)10, you'll get the ASCII equivalent of 10, which is neither A,B or C. Hence the compiler chooses "default", and prints its value. But then, since you haven't mentioned the break; statement after default, it continues to execute other statements in the switch case bundle [till end, or until it encounters a break], and hence you are getting this output.

Dawn.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:



Yet this does not address the question of using the static class varibles decalred. In this statement you are simply casting the local varibles to a char.

I tried this way:




I recieved the following output:



What function do you give to your final local variables in your original code?



 
reply
    Bookmark Topic Watch Topic
  • New Topic