-4
import java.io.*;
public class Magic{

   static final int maxsize = 50;

    public static void main (String [] args) throws IOException{

      int i, j, k, l, n, key;
      boolean n_ok;
      String line;
      int [] [] square = new int [maxsize] [maxsize];

      BufferedReader KeybIn = new BufferedReader(new InputStreamReader(System.in));

      try{


         System.out.print("Size of square? ");
         line  = KeybIn.readLine();
         n = Integer.parseInt(line);

         n_ok = (1<=n) & (n<=maxsize+1) & (n%2==1);


         if ( n_ok ){


            for (i=0;i<n;i++)
               for (j=0;j<n;j++) square[i][j] = 0;
            square[0][(int)(n-1)/2] = 1;

            key = 2;
            i = 0;
            j = (int)(n-1)/2;
            while ( key <= n*n ){

               k = i - 1;

               if ( k < 0 ) k = k + n;
               l = j - 1;

               if ( l < 0 ) l = l + n;

               if ( square[k][l] != 0 ) i = (i+1) % n;

               else { i = k; j = l; }
               square[i][j] = key;
               key = key + 1;
            }


            System.out.println("Magic square of size " + n);

            for (i=0;i<n;i++)

            {

               for (j=0;j<n;j++)
                  System.out.print("\t"+square[i][j]);
               System.out.println();
            }
         }      
      }catch (NumberFormatException e){

         System.out.println("Error in number, try again.");

      }

   }
}

So how do I put the "Try again yes or no"? just that.. then if I enter y .. it will ask the user the size of the square again .. if letter n it will exit .. this is for magic square

false
  • 10,264
  • 13
  • 101
  • 209
  • 1
    Welcome to Stack Overflow! This is not a forum; __this is a questions and answer site, and I'm afraid your post isn't really a question, because [real questions have answers, not items or ideas or opinions](http://blog.stackoverflow.com/2011/01/real-questions-have-answers/).__ [Please read the FAQ for more information.](http://stackoverflow.com/faq) –  Sep 30 '11 at 13:30
  • 1
    I disagree with the close. I think this is a real question, and has a real answer. – Erick Robertson Sep 30 '11 at 13:53
  • @ErickRobertson On the other hand, 8 years later, that low quality question has (almost) only attracted low quality answers. This question is a good example why to not tolerate low quality input, be it questions or answers.... – GhostCat Nov 13 '19 at 08:32
  • @GhostCatsalutesMonicaC.are you suggesting someone who is learning to program and finds this page wouldn't benefit from reading through all the answers? – Erick Robertson Feb 04 '20 at 18:53

4 Answers4

3
String tryAgain = "y";
do
{
   // you code

   System.out.println("Try again? enter \"y/n\".");
   tryAgain = System.in.readLine();

}
while(!tryAgain.equals("n"));
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • @JefframCatapang the do-while show wrap around the code you have. – Bala R Sep 30 '11 at 13:36
  • @JefframCatapang offtopic, but doing everything in the main method is not a good design. Try to at atleast split things into a few methods. – Bala R Sep 30 '11 at 13:38
0

Put your code that computes your magic square in a separate method, and have your input reading code in a while loop, that calls that method until the user presses N, for example.

JRL
  • 76,767
  • 18
  • 98
  • 146
0

Wrap the try/catch statement with a while loop.

while(not true) {
    do foo()
}
FloppyDisk
  • 1,693
  • 16
  • 25
-1

Check out the Scanner class.

Kris
  • 5,714
  • 2
  • 27
  • 47
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. –  Sep 30 '11 at 13:53