1

I am very new to Java Programming, I have been given a task to work out from user input the easter date for a given year and then to ask if the user would like to input another year to calculate another easter date. What I don't understand is how to create a loop from the second input box ("Noch eine Berechnung? (J/N)") that repeats the entire process again. My code is this:

import javax.swing.JOptionPane;

public class Osterberechnung_ohne_schleife {

 public static void main(String[] args) {


  String sJahrEingabe = JOptionPane.showInputDialog("Bitte Jahr eingeben:");


  int iJahr = Integer.parseInt(sJahrEingabe);

  int iP = iJahr / 100;
  int iQ = iP / 3;
  int iR = iQ / 4;
  int iX = (15 + iP - iQ - iR) % 30;
  int iY = (iP + 4 - iR) % 7;
  int iA = iJahr % 19;
  int iB = iJahr % 4;
  int iC = iJahr % 7;
  int iD = (19 * iA + iX) % 30;
  int iE = (2 * iB + 4 * iC + 6 * iD + iY) % 7;



  if ((iD == 29) && (iE == 6)) {

   System.out.println("Ausgabe: Ostern ist am 19.April");

  } else if ((iD == 28) && (iE == 6)) {

   System.out.println("Ausgabe: Ostern ist am 18.April");

  } else if ((22 + iD + iE) <= 31) {

   System.out.println("Ausgabe: Ostern ist am " + (22 + iD + iE) + ".März");

  } else {

   System.out.println("Ausgabe: Ostern ist am " + (iD + iE - 9) + ".April");

  }

  {

   String sNochJahr = JOptionPane.showInputDialog("Noch eine Berechnung? (J/N)");

  }


 }

}

Thanks in advance for your help.

2
  • Try putting your whole code in a WHILE loop. If user enters 'N', then break out of that loop Commented Nov 17, 2017 at 13:30
  • Put the code that does the computation in a function, then create a loop that will call it as long as then answer is J. Commented Nov 17, 2017 at 13:32

2 Answers 2

1

Surround your code with a do...while loop and keep going as long as sNochJahr is J.

do {
...
} while (sNochJahr.equals("J"));

or while sNochJahr is not N.

while (!sNochJahr.equals("N"));
Sign up to request clarification or add additional context in comments.

4 Comments

I tried to put the whole code into a do...while loop, however the while sNochJahr cannot be resolved because the sNochjahr variable is within the do...while code. What i don't understand is how to make the do...while loop determined by the user input within the code.
do { if ((iD==29) && (iE==6)) { System.out.println("Ausgabe: Ostern ist am 19.April"); } else if ((iD==28) && (iE==6)) { System.out.println("Ausgabe: Ostern ist am 18.April"); } else if ((22 +iD + iE) <= 31) { System.out.println("Ausgabe: Ostern ist am " + (22 + iD + iE) + ".März"); } else { System.out.println("Ausgabe: Ostern ist am " + (iD + iE - 9) + ".April"); } String sNochJahr = JOptionPane.showInputDialog("Noch eine Berechnung? (J/N)"); } while(sNochJahr.equals("J")); } }
Copy the code from that link there. Don't mind the error it's showing on that site; it's to do with JOptionPane.
Thank you very much, it worked. Thanks for explaining, I understand now what I was doing wrong.
1

Youre saving the response of the user in String sNochJahr, but not evaluating it. I would suggest something like this:

String sNochJahr = "J"

while (sNochJahr.equals("J")) {
      String sJahrEingabe = JOptionPane.showInputDialog("Bitte Jahr eingeben:");


    int iJahr = Integer.parseInt(sJahrEingabe);

    int iP = iJahr / 100;
    int iQ = iP / 3;
    int iR = iQ / 4;
    int iX = (15 + iP - iQ - iR)%30;
    int iY = (iP + 4 - iR)%7;
    int iA = iJahr % 19;
    int iB = iJahr % 4;
    int iC = iJahr % 7;
    int iD = (19 * iA + iX)%30;
    int iE = (2 * iB + 4 * iC + 6 * iD + iY)%7;



    if ((iD==29) && (iE==6)) {

        System.out.println("Ausgabe: Ostern ist am 19.April");

    } else if ((iD==28) && (iE==6)) {

        System.out.println("Ausgabe: Ostern ist am 18.April");

    } else if ((22 +iD + iE) <= 31) {

        System.out.println("Ausgabe: Ostern ist am " + (22 + iD + iE) + ".März");

    } else {

        System.out.println("Ausgabe: Ostern ist am " + (iD + iE - 9) + ".April");

    }

    {

    String sNochJahr = JOptionPane.showInputDialog("Noch eine Berechnung? (J/N)");

        }
}

If the user inputs "N" then, the code breaks out of the while-loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.