Search code examples
javastringjoptionpaneparseint

JOptionPane, help on return


this is my first post on this website, as this website has been a lot of help. I have came up with not so much a problem, but something I want to learn on how to do. Here is my code

String a = JOptionPane.showInputDialog(null,"Please pick something for me to do master:\nMynumber,Read2me, Conversions");
if (a.equals("Mynumber"))
{
    MyNumber = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter your number: "));
    String b = JOptionPane.showInputDialog(null,"Was Your number "+MyNumber+"\n Y/N");
    if (b.equals("y"))
    {
        JOptionPane.showMessageDialog(null,"Good...good, now lets play with\n your number");
    }
    else if(b.equals("N"))
    {
        JOptionPane.showMessageDialog(null,"returning");

My goal is to when it gets to the last on (when the user types n) I want it to return to the starting String, or String A, how would I implement this into my code


Solution

  • Add a loop to your code, for example

    while(true) { // instead of while(true) you can also write other condition
     String a = JOptionPane.showInputDialog(null,"Please pick something for me to do  master:\nMynumber,Read2me, Conversions");
     if (a.equals("Mynumber")) {
        MyNumber = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter your number: "));
        String b = JOptionPane.showInputDialog(null,"Was Your number "+MyNumber+"\n Y/N");
        if (b.equals("y")) {
            JOptionPane.showMessageDialog(null,"Good...good, now lets play with\n your number");
        }
        else if(b.equals("N")) {
            JOptionPane.showMessageDialog(null,"returning");
        }
     }
    }