Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Non-readable case statements help.

    In the code, after the beginning sequence there is a huge case statement around the whole code, and java can't read the 2nd or 3rd case statement but can read the first. I have no idea why it's not working, anything will help.

    code:
    Attached Files Attached Files

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,722
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Try to keep this in one thread- This is the third thread on this o.0... Anyways, I think your problem is you are trying to go way to complex before you understand what is going on in the code. I don't have eclipse on my work computer, so this is in C#, but the readability should be easy... Start simple... Work with this structure, and get it working and then go deeper. I know the switch statement is redundant, BUT- when you start to elaborate on it it will be the groundwork laid out for you.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Help
    {
        class Program
        {
            static void Main(string[] args)
            {
                demo d = new demo();
                Console.ReadLine();
            }
        }
    
    
        class demo
        {
    
            string[] questions = new string[] { "What color is a banana?", "What color is an orange?", "What color is a blueberry?", "What color is a strawberry?", "What color is a white grape?"};
            string[] answers = new string[] { "yellow", "orange", "blue", "red", "green" };
            enum answerConversion { yellow = 0, orange = 1, blue = 2, red = 3, green = 4};
            int selection;
            int pointer;
            Random rndm = new Random();
    
            public demo()
            {
                pointer = rndm.Next(0, 4);
                Console.WriteLine("Hello, welcome to the color game...\n{0}", questions[pointer]);
    
                try
                {
                    selection = Convert.ToInt16((answerConversion)Enum.Parse(typeof(answerConversion), Console.ReadLine()));
                }
    
                catch
                {
                    selection = 5;
                }
    
                switch (selection)
                {
                    case 0:
                        if (selection == pointer) Console.WriteLine("That is correct!");
                        else goto default;
                        break;
                    case 1:
                        if (selection == pointer) Console.WriteLine("That is correct!");
                        else goto default;
                        break;
                    case 2:
                        if (selection == pointer) Console.WriteLine("That is correct!");
                        else goto default;
                        break;
                    case 3:
                        if (selection == pointer) Console.WriteLine("That is correct!");
                        else goto default;
                        break;
                    case 4:
                        if (selection == pointer) Console.WriteLine("That is correct!");
                        else goto default;
                        break;
                    default:
                        Console.WriteLine("That is incorrect.");
                        break;
                }
    
            }
        }
    }
    The console.WriteLine() and Console.Readline() are equivalent to your System.out.println and Scanner. The part I highlighted in red is converting text to an enumeration to an integer, it is a lot easier IMO to use. I do not know how to do this in Java, it allows you to work with a primitive type though instead of cross-checking strings though.

    Post follow-up questions on this thread though instead of making new ones :P Good luck! play around with this construct some it should help you.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •