I am trying to program a very simple 3x3 magic square game using arrays.
My program will ask the user thrice to input three numbers each time (for the top row, middle row, bottom row) and then the values will be displayed. The problem is I'm trying to get my program to verify if the user entered the correct values.
I'm not sure how I will do it; I've thought of simply declaring fixed values and just make comparisons/use conditional statements to compare the user's input to those constant values (explains arrays d, e, & f in my code below), but I also thought that that's not rather completely correct since there are many possible values that will be able to make up a magic square, aside from the values I'm going to declare, and of course, I would not want to be that redundant in my code.
I'm not that familiar with arrays yet, I'm a beginner in Java. I'm not sure if it's okay to ask that question on here, but I hope someone will still respond. This is rather important since it's a school project.
I know that the numbers should have a total of 15 horizontally, vertically and diagonally. And I have no idea how to do that.
Although, I guess I would just stick to simply making conditional statements (if no one will answer my question above) since I just found a source that has all the 3x3 magic squares, so if someone could just teach me how to compare user-defined arrays & programmer-defined arrays, that would be nice.
I have written some codes to store the user input.
import java.util.*;
class arrayy
{
public static void main(String args[])
{
int[] a=new int[3];
int[] b=new int[3];
int[] c=new int[3];
int[] d={8,1,6};
int[] e={3,5,7};
int[] f={4,9,2};
Scanner sc=new Scanner(System.in);
System.out.println("Please enter the 3 numbers for the top row: ");
for(int j=0;j<3;j++)
a[j]=sc.nextInt();
System.out.println();
System.out.println("Please enter the 3 numbers for the middle row: ");
for(int j=0;j<3;j++)
b[j]=sc.nextInt();
System.out.println();
System.out.println("Please enter the 3 numbers for the bottom row: ");
for(int j=0;j<3;j++)
c[j]=sc.nextInt();
System.out.println("Your entry: ");
for (int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
for (int i=0;i<a.length;i++)
System.out.print(b[i]+" ");
System.out.println();
for (int i=0;i<a.length;i++)
System.out.print(c[i]+" ");
System.out.println();
System.out.println();
}
}
assuming that you have read the values into the array the following code will work for all the combinations that could solve the magic square