# can i do this with switch statement?

Printable View

• March 6th, 2014, 01:25 PM
m7abraham
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"
Code :

```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
• March 6th, 2014, 01:58 PM
KevinWorkman
Re: can i do this with switch statement?
What have you tried?