Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: can i do this with switch statement?

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default can i do this with switch statement?

    So i have this code and out of curiosity I was wondering if it would be better to use "switch statement"
    import java.util.Random;
    import javax.swing.JOptionPane;
     
    public class Main {
     
        public static void main(String[] args) {
     
            String c, n;
     
            Random r = new Random();
            int rn = r.nextInt(1000) + 1;
     
            c = JOptionPane.showInputDialog("Enter a choice:\n1--Enter the number"
                    + "\n2--Create a random number");
            int cc = Integer.parseInt(c);
     
     
            if (cc == 1) {
                n = JOptionPane.showInputDialog("Enter a number between 1 - 1000");
                int nn = Integer.parseInt(n);
     
                if (nn > 1000) {
                    JOptionPane.showMessageDialog(null, "Invalid choice");
                } else {
                    if (nn % 3 == 0) {
                        JOptionPane.showMessageDialog(null, "The nuber " + nn + " is "
                                + "divisable by 3");
                    } else {
                        JOptionPane.showMessageDialog(null, "The nuber " + nn + " Is NOT"
                                + " divisable by 3");
                    }
                }
     
     
            } else if (cc == 2) {
                if (rn % 3 == 0) {
                    JOptionPane.showMessageDialog(null, "The number " + rn + " is"
                            + " divisable by 3");
                } else {
                    JOptionPane.showMessageDialog(null, "The number " + rn + " is NOT"
                            + " divisable by 3");
                }
     
     
            } else {
                JOptionPane.showMessageDialog(null, "Invalid choice");
            }
        }
    }

    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: can i do this with switch statement?

    What have you tried?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    m7abraham (March 7th, 2014)

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 03:19 PM
  2. for-each loop and a switch statement
    By Grot in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 4th, 2013, 01:02 PM
  3. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 3rd, 2013, 11:21 PM
  4. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM