尝试为我的Java类创建一个神奇的方形游戏。我第一次看到了以下代码:
import java.util.Arrays;
import java.util.Scanner;
public class square {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String mainMenuUser = null;
while(mainMenuUser != "quit"){
System.out.println();
System.out.println(" Welcome to Magic Square! ");
System.out.println("--------------------------");
System.out.println("Type any of the following:");
System.out.println("--------------------------");
System.out.println("Rules (Displays Rules)");
System.out.println("Play (Starts The Game)");
System.out.println("Quit (Quits The Game)");
System.out.println("--------------------------");
System.out.print("Your Choice: ");
mainMenuUser = scanner.next();
switch(mainMenuUser.toLowerCase()){
case "rules":
rules();
break;
case "play":
play(null);
break;
case "quit":
break;
default:
System.out.println("You Typed something wrong.");
break;
}
}
}
public static void rules(){
System.out.println("The Rules Are: ");
System.out.println("1: All numbers must be different");
System.out.println(" It will tell you if you have duplicates.");
System.out.println("2: All numbers must add up to be the same Left to right");
System.out.println("3: All numbers must add up to be the same Top to Bottom");
System.out.println("4: All numbers must add up to be the same Diagonal Top left to Bottom Right");
System.out.println("4.A: All numbers must add up to be the same Diagonal Top Right to Bottom Left");
}
public static void play(int[] rowTotalsLR){
int userGridSize;
int tempMultiply;
System.out.println("Please Enter a number for the size of the grid.");
System.out.println("This number will be squared");
userGridSize = scanner.nextInt();
tempMultiply = userGridSize * userGridSize;
System.out.println("Your Grid Size Will Be: " + tempMultiply);
takeInGridNums(userGridSize, rowTotalsLR);
}
public static void takeInGridNums(int userGridSize, int[] rowTotalsLR){
int gridRows = 0, gridColumns = 0;
int[][] gridNums = new int[userGridSize][userGridSize];
int tempSize = gridNums.length;
int tempSizeFull = tempSize * tempSize;
for(int temp = 0; temp != tempSizeFull; temp++){
if(gridColumns == gridNums.length){gridRows++; gridColumns = 0;}
System.out.println("Please Eneter A number for Grid Position: " + gridRows + " , " + gridColumns);
gridNums[gridRows][gridColumns] = scanner.nextInt();
gridColumns++;
}
leftToRight(tempSizeFull, gridNums, tempSizeFull);
topToBottom(tempSizeFull, gridNums, tempSizeFull);
diagonalLeftToRight(userGridSize, gridNums, tempSizeFull);
diagonalRightToLeft(userGridSize, gridNums, tempSizeFull);
outputResultsOfGame(tempSizeFull, gridNums, rowTotalsLR, null, tempSizeFull, tempSizeFull, tempSizeFull, null, null);
}
public static void leftToRight(int userGridSize, int[][] gridNums, int tempSizeFull){
int tempGridColumns = 0, totalLeftToRight = 0, tempPoint, tempTotal = 0, tempGridRows = 0;
int[] rowTotalsLR = new int[userGridSize];
for(int temp = 0; temp != tempSizeFull; temp++){
if(tempGridColumns == gridNums.length){
rowTotalsLR[tempGridRows] = totalLeftToRight;
tempGridRows++;
tempGridColumns = 0;
tempTotal = 0;
totalLeftToRight = 0;
}
tempPoint = gridNums[tempGridRows][tempGridColumns];
totalLeftToRight = tempTotal + tempPoint;
tempTotal = totalLeftToRight;
tempGridColumns++;
}
}
public static void topToBottom(int userGridSize, int[][] gridNums, int tempSizeFull){
int tempGridColumns = 0, totalRightToLeft = 0, tempPoint = 0, tempTotal = 0, tempGridRows = 0;
int[] rowTotalsRL = new int[userGridSize];
for(int temp = 0; temp != tempSizeFull; temp++){
if(tempGridRows == gridNums.length){
rowTotalsRL[tempGridRows] = totalRightToLeft;
tempGridColumns++;
tempGridRows = 0;
tempTotal = 0;
totalRightToLeft = 0;
}
tempPoint = gridNums[tempGridRows][tempGridColumns];
totalRightToLeft = tempTotal + tempPoint;
tempTotal = totalRightToLeft;
tempGridRows++;
}
}
public static void diagonalLeftToRight(int userGridSize, int[][] gridNums, int tempSizeFull){
int totalDagLeftToRight = 0, tempPoint = 0, tempTotal = 0;
int[] totalsDagLR = new int[userGridSize];
for(int temp = 0; temp < tempSizeFull; temp++){
//for(int temp = 0; temp != gridNums.length; temp++){
if(temp == gridNums.length){break;}
tempPoint = gridNums[temp][temp];
totalDagLeftToRight = tempTotal + tempPoint;
tempTotal = totalDagLeftToRight;
totalsDagLR[temp] = tempTotal;
}
}
public static void diagonalRightToLeft(int userGridSize, int[][] gridNums, int tempSizeFull){
int totalDagLeftToRight = 0, tempPoint = 0, tempTotal = 0, tempUp = 0, tempDown = gridNums.length-1;
int[] totalsDagRL = new int[userGridSize];
for(int temp = 0; temp != gridNums.length; temp++){
tempPoint = gridNums[tempUp][tempDown];
totalDagLeftToRight = tempTotal + tempPoint;
tempTotal = totalDagLeftToRight;
totalsDagRL[temp] = tempTotal;
}
}
public static void outputResultsOfGame(int userGridSize, int[][] gridNums, int[] rowTotalsLR, int[] rowTotalsRL, int gridRows, int gridColumns, int tempSizeFull, int[] totalsDagLR, long[] totalsDagRL){
System.out.println("Your Numbers Entered Were: ");
System.out.println(Arrays.deepToString(gridNums));
System.out.println("The Totals of the Rows Are: ");
System.out.println(Arrays.toString(rowTotalsLR));
System.out.println("The Total of the Columns Are: ");
System.out.println(Arrays.toString(rowTotalsRL));
System.out.println("The Total of The Top left to Bottom Right Diagonal is: ");
System.out.println(Arrays.toString(totalsDagLR));
System.out.println("The Total of The Top Right to Bottom Left Diagonal is: ");
System.out.println(Arrays.toString(totalsDagRL));
}
}
我的问题是输出以null
结束所有数学方法。在看到我决定我需要修改我的代码并使其更有条理之后。我在下面做过的;
编辑此代码2/21/15美国东部时间下午10:29
import java.util.Arrays;
import java.util.Scanner;
public class testingChanges {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String mainMenuUser = "";
int userGridSize = 0, gridRows = 0, gridColumns = 0, totalRightToLeft = 0, tempGridColumns = 0, totalLeftToRight = 0, tempPoint = 0, tempTotal = 0, tempGridRows = 0, totalDagLeftToRight = 0;
int[][] gridNums = new int[userGridSize][userGridSize];
int tempSize = gridNums.length, tempUp = 0, tempDown = gridNums.length-1;
int tempMultiply = userGridSize * userGridSize;
int[] rowTotalsLR = new int[userGridSize];
int[] rowTotalsRL = new int[userGridSize];
int[] totalsDagLR = new int[userGridSize];
int[] totalsDagRL = new int[userGridSize];
while(mainMenuUser != "quit"){
System.out.println();
System.out.println(" Welcome to Magic Square! ");
System.out.println("--------------------------");
System.out.println("Type any of the following:");
System.out.println("--------------------------");
System.out.println("Rules (Displays Rules)");
System.out.println("Play (Starts The Game)");
System.out.println("Quit (Quits The Game)");
System.out.println("--------------------------");
System.out.print("Your Choice: ");
mainMenuUser = scanner.next();
switch(mainMenuUser.toLowerCase()){
case "rules":
rules();
break;
case "play":
play(userGridSize, tempMultiply, rowTotalsLR, tempGridColumns, gridNums, tempGridRows, totalLeftToRight, tempTotal, tempPoint, gridColumns, gridRows, rowTotalsRL, totalRightToLeft, totalDagLeftToRight, totalsDagLR, totalsDagRL, tempUp, tempDown);
break;
case "quit":
break;
default:
System.out.println("You Typed something wrong.");
break;
}
}
}
public static void rules(){
System.out.println("The Rules Are: ");
System.out.println("1: All numbers must be different");
System.out.println(" It will tell you if you have duplicates.");
System.out.println("2: All numbers must add up to be the same Left to right");
System.out.println("3: All numbers must add up to be the same Top to Bottom");
System.out.println("4: All numbers must add up to be the same Diagonal Top left to Bottom Right");
System.out.println("4.A: All numbers must add up to be the same Diagonal Top Right to Bottom Left");
}
public static void play(int userGridSize, int tempMultiply, int[] rowTotalsLR, int tempGridColumns, int[][] gridNums, int tempGridRows, int totalLeftToRight, int tempTotal, int tempPoint, int gridColumns, int gridRows, int[] rowTotalsRL, int totalRightToLeft, int totalDagLeftToRight, int[] totalsDagLR, int[] totalsDagRL, int tempUp, int tempDown){
System.out.println("Please Enter a number for the size of the grid.");
System.out.println("This number will be squared");
userGridSize = scanner.nextInt();
gridNums = new int[userGridSize][userGridSize];
tempMultiply = userGridSize * userGridSize;
System.out.println("Your Grid Size Will Be: " + tempMultiply);
takeInGridNums(gridColumns, gridRows, gridNums, tempGridColumns, tempGridRows, totalLeftToRight, rowTotalsLR, tempTotal, tempPoint, rowTotalsRL, totalRightToLeft, totalsDagLR, totalDagLeftToRight, totalsDagRL, tempUp, tempDown, tempDown);
}
public static void takeInGridNums(int gridColumns, int gridRows, int[][] gridNums, int tempGridColumns, int tempGridRows, int totalLeftToRight, int[] rowTotalsLR, int tempTotal, int tempPoint, int[] rowTotalsRL, int totalRightToLeft, int[] totalsDagLR, int totalDagLeftToRight, int[] totalsDagRL, int tempUp, int tempDown, int tempMultiply){
System.out.println("INSIDE OF TAKE IN");
System.out.println(tempMultiply);
for(int temp = 0; temp != tempMultiply; temp++){
if(gridColumns == gridNums.length){gridRows++; gridColumns = 0;}
System.out.println("Please Eneter A number for Grid Position: " + gridRows + " , " + gridColumns);
gridNums[gridRows][gridColumns] = scanner.nextInt();
gridColumns++;
}
leftToRight(tempGridColumns, tempMultiply, gridNums, tempGridRows, totalLeftToRight, rowTotalsLR, tempTotal, tempPoint);
topToBottom(tempGridRows, tempMultiply, gridNums, rowTotalsRL, totalRightToLeft, tempGridColumns, tempTotal, tempPoint);
diagonalLeftToRight(tempMultiply, gridNums, tempPoint, totalDagLeftToRight, tempTotal, totalsDagLR);
diagonalRightToLeft(gridNums, tempPoint, totalDagLeftToRight, tempTotal, totalsDagRL, tempUp, tempDown);
outputResultsOfGame(gridNums, rowTotalsLR, rowTotalsRL, totalsDagLR, totalsDagRL);
}
public static void leftToRight(int tempMultiply, int tempGridColumns, int[][] gridNums, int tempGridRows, int totalLeftToRight, int[] rowTotalsLR, int tempTotal, int tempPoint){
for(int temp = 0; temp != tempMultiply; temp++){
if(tempGridColumns == gridNums.length){
rowTotalsLR[tempGridRows] = totalLeftToRight;
tempGridRows++;
tempGridColumns = 0;
tempTotal = 0;
totalLeftToRight = 0;
}
tempPoint = gridNums[tempGridRows][tempGridColumns];
totalLeftToRight = tempTotal + tempPoint;
tempTotal = totalLeftToRight;
tempGridColumns++;
}
}
public static void topToBottom(int tempMultiply, int tempGridRows, int[][] gridNums, int[] rowTotalsRL, int totalRightToLeft, int tempGridColumns, int tempTotal, int tempPoint){
for(int temp = 0; temp != tempMultiply; temp++){
if(tempGridRows == gridNums.length){
rowTotalsRL[tempGridRows] = totalRightToLeft;
tempGridColumns++;
tempGridRows = 0;
tempTotal = 0;
totalRightToLeft = 0;
}
tempPoint = gridNums[tempGridRows][tempGridColumns];
totalRightToLeft = tempTotal + tempPoint;
tempTotal = totalRightToLeft;
tempGridRows++;
}
}
public static void diagonalLeftToRight(int tempMultiply, int[][] gridNums, int tempPoint, int totalDagLeftToRight, int tempTotal, int[] totalsDagLR){
for(int temp = 0; temp < tempMultiply; temp++){
//for(int temp = 0; temp != gridNums.length; temp++){
if(temp == gridNums.length){break;}
tempPoint = gridNums[temp][temp];
totalDagLeftToRight = tempTotal + tempPoint;
tempTotal = totalDagLeftToRight;
totalsDagLR[temp] = tempTotal;
}
}
public static void diagonalRightToLeft(int[][] gridNums, int tempPoint, int totalDagLeftToRight, int tempTotal, int[] totalsDagRL, int tempUp, int tempDown){
for(int temp = 0; temp != gridNums.length; temp++){
tempPoint = gridNums[tempUp][tempDown];
totalDagLeftToRight = tempTotal + tempPoint;
tempTotal = totalDagLeftToRight;
totalsDagRL[temp] = tempTotal;
}
}
public static void outputResultsOfGame(int[][] gridNums, int[] rowTotalsLR, int[] rowTotalsRL, int[] totalsDagLR, int[] totalsDagRL){
System.out.println("Your Numbers Entered Were: ");
System.out.println(Arrays.deepToString(gridNums));
System.out.println("The Totals of the Rows Are: ");
System.out.println(Arrays.toString(rowTotalsLR));
System.out.println("The Total of the Columns Are: ");
System.out.println(Arrays.toString(rowTotalsRL));
System.out.println("The Total of The Top left to Bottom Right Diagonal is: ");
System.out.println(Arrays.toString(totalsDagLR));
System.out.println("The Total of The Top Right to Bottom Left Diagonal is: ");
System.out.println(Arrays.toString(totalsDagRL));
}
}
然后该代码以takeInGridNums(..);
方法结束,甚至没有询问我的输入。我知道我的所有变量都在main中可能很奇怪,但我只是想通过在那里拥有所有需要的变量来解决问题并使事情更简单,并将它们传递给我的方法。我也试图避免使用全局变量。我确实使用了一个用于我的扫描仪,因为这只是一件容易的事情。任何帮助使这两个代码中的任何一个起作用都会非常感激。
答案 0 :(得分:0)
在第二组代码中,您的takeInGridNums(..)
方法正在调用,但您正在创建tempSizeFull
大的网格。但是,您的tempSizeFill
变量为0,因为您将其基于主userGridSize
并且初始化为0.当用户输入他们想要的网格大小时,{{1不会自动更新,因此会跳过您的tempSizeFull
循环。
所以而不是:
for
尝试类似:
userGridSize = scanner.nextInt();
tempMultiply = userGridSize * userGridSize;
System.out.println("Your Grid Size Will Be: " + tempMultiply);
takeInGridNums(gridColumns, tempSizeFull, gridRows, gridNums, tempGridColumns, tempGridRows, totalLeftToRight, rowTotalsLR, tempTotal, tempPoint, rowTotalsRL, totalRightToLeft, totalsDagLR, totalDagLeftToRight, totalsDagRL, tempUp, tempDown);`
答案 1 :(得分:0)
如果进行以下更改:
ArrayList<ArrayList<Integer>> gridNums = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> rowTotalsLR = new ArrayList<Integer>();
ArrayList<Integer> rowTotalsRL = new ArrayList<Integer>();
ArrayList<Integer> totalsDagLR = new ArrayList<Integer>();
ArrayList<Integer> totalsDagRL = new ArrayList<Integer>();
循环看起来像: int numCols =(int)Math.sqrt(tempMultiply); for(int temp = 0; temp!= tempMultiply; temp ++){ if(gridColumns == numCols){gridRows ++; gridColumns = 0;} System.out.println(“请输入网格位置的数字:”+ gridRows +“,”+ gridColumns); gridNums.get(gridRows)。新增(scanner.nextInt()); gridColumns ++; }
然后,您可以进行类似的更改,以便从数组更改为ArrayList
答案 2 :(得分:0)
重写完整个代码后,我终于完成了一个成品。基本上我摆脱了所有传递引用并创建了实例变量。我通过电子邮件向我的Java老师询问使用实例变量是否正常后,我做出了这个决定。她确认并说我应该为这个项目使用实例变量。这导致我决定再次编写整个程序,在下面创建。感谢所有试图提供帮助的人,但我之前的代码实在是太过分了。
目前的工作计划:
import java.util.Arrays;
import java.util.Scanner;
public class squareAgain {
static Scanner scan = new Scanner(System.in);
static int[][] userSquare = new int[0][0];
static int[] mathLRTotals = new int[0], mathTBTotals = new int[0], mathDLRTotals = new int[1], mathDRLTotals = new int[1];
static String userMenuInput = "presetByProgrammer";
static int totalElements = 0;
public static void main(String[] args) {
while(userMenuInput != "quit"){
mainMenu();
System.out.print("Your Choice: ");
userMenuInput = scan.next();
switch(userMenuInput.toLowerCase()){
case "rules":
menuOptionRules();
break;
case "play":
menuOptionPlay();
break;
case "quit":
System.out.println("Now Quitting.");
userMenuInput = "quit";
break;
default:
System.out.println("You Typed something wrong.");
break;
}
}
}
private static void mainMenu(){
System.out.println(" Welcome to Magic Square! ");
System.out.println("--------------------------");
System.out.println("Type any of the following:");
System.out.println("--------------------------");
System.out.println("Rules (Displays Rules)");
System.out.println("Play (Starts The Game)");
System.out.println("Quit (Quits The Game)");
System.out.println("--------------------------");
}
private static void menuOptionRules(){
System.out.println("The Rules Are: ");
System.out.println("1: All numbers must be different");
System.out.println(" It will tell you if you have duplicates.");
System.out.println("2: All numbers must add up to be the same Left to right");
System.out.println("3: All numbers must add up to be the same Top to Bottom");
System.out.println("4: All numbers must add up to be the same Diagonal Top left to Bottom Right");
System.out.println("4.A: All numbers must add up to be the same Diagonal Top Right to Bottom Left");
}
private static void menuOptionPlay(){
getUserInputs();
mathLR();
mathTB();
mathDLR();
mathDRL();
outputs();
}
private static void getUserInputs(){
System.out.println("Thank You For Playing.");
System.out.print("Please Enter A Size For Your Square: ");
int userSquareSizeInput = scan.nextInt();
userSquare = new int[userSquare.length+userSquareSizeInput][userSquare.length+userSquareSizeInput];
System.out.println("Array Length is now: " + userSquare.length);
totalElements = userSquareSizeInput * userSquareSizeInput;
System.out.println("This Means You Will Have " + totalElements + " elements." );
System.out.println();
int tempForGridInputRow = 0, tempForGridInputCol = 0;
for(int temp = 0; temp != totalElements; temp++){
if(tempForGridInputCol == userSquare.length){tempForGridInputRow++; tempForGridInputCol = 0;}
System.out.print("Please Eneter A number for Grid Position: " + tempForGridInputRow + " , " + tempForGridInputCol + ": ");
userSquare[tempForGridInputRow][tempForGridInputCol] = scan.nextInt();
tempForGridInputCol++;
}
System.out.println();
}
private static void mathLR(){
mathLRTotals = new int[mathLRTotals.length+userSquare.length];
int tempForGridInputRow = 0, tempForGridInputCol = 0, tempElement = 0, tempTotal = 0;
for(int temp = 0; temp < totalElements; temp++){
tempElement = tempElement + userSquare[tempForGridInputRow][tempForGridInputCol];
tempTotal = tempElement;
tempForGridInputCol++;
if(tempForGridInputCol == userSquare.length){
mathLRTotals[tempForGridInputRow] = tempTotal;
tempForGridInputRow++;
tempForGridInputCol = 0;
tempTotal = 0;
tempElement = 0;
}
}
}
private static void mathTB(){
mathTBTotals = new int[mathTBTotals.length+userSquare.length];
int tempForGridInputRow = 0, tempForGridInputCol = 0, tempElement = 0, tempTotal = 0;
for(int temp = 0; temp < totalElements; temp++){
tempElement = tempElement + userSquare[tempForGridInputRow][tempForGridInputCol];
tempTotal = tempElement;
tempForGridInputRow++;
if(tempForGridInputRow == userSquare.length){
mathTBTotals[tempForGridInputCol] = tempTotal;
tempForGridInputRow = 0;
tempForGridInputCol++;
tempTotal = 0;
tempElement = 0;
}
}
}
private static void mathDLR(){
int tempElement = 0;
for(int temp = 0; temp != userSquare.length; temp ++){
tempElement = tempElement + userSquare[temp][temp];
mathDLRTotals[0] = tempElement;
}
}
private static void mathDRL(){
int tempForGridInputRow = 0, tempForGridInputCol = userSquare.length-1, tempElement = 0;
for(int temp = 0; temp != userSquare.length; temp ++){
tempElement = tempElement + userSquare[tempForGridInputRow][tempForGridInputCol];
mathDRLTotals[0] = tempElement;
tempForGridInputRow++;
tempForGridInputCol--;
}
}
private static void outputs(){
System.out.println("You Entered " + userSquare.length + " As your grid length and height");
System.out.println("This means there was " + totalElements + " elements in your square.");
System.out.println("Here is what you entered: ");
System.out.println(Arrays.deepToString(userSquare));
System.out.println("The totals for each row are: ");
System.out.println(Arrays.toString(mathLRTotals));
System.out.println("The totals for each column are: ");
System.out.println(Arrays.toString(mathTBTotals));
System.out.println("The total of the Left to Right Diagonal: ");
System.out.println(Arrays.toString(mathDLRTotals));
System.out.println("The total of Right to Left Diagonal: ");
System.out.println(Arrays.toString(mathDRLTotals));
System.out.println();
String tempYesForLR = "setByProgrammer";
int tempCompareLR = mathLRTotals[0];
for(int temp = 0; temp != mathLRTotals.length; temp++){
if(tempCompareLR == mathLRTotals[temp]){tempYesForLR = "true";}else{tempYesForLR = "false"; break;}
}
String tempYesForTB = "setByProgrammer";
int tempCompareTB = mathTBTotals[0];
for(int temp = 0; temp != mathTBTotals.length; temp++){
if(tempCompareTB == mathTBTotals[temp]){tempYesForTB = "true";}else{tempYesForTB = "false"; break;}
}
Boolean tempForYesDag = false;
if(mathDLRTotals[0] == mathDRLTotals[0]){tempForYesDag = true;}
if(tempYesForLR == tempYesForTB == tempForYesDag){
System.out.println("All of the rows added up to be: " + mathLRTotals[0]);
System.out.println("You created a Perfect Square!");
System.out.println();
}
else{
System.out.println("Your rows didn't add up to the same number.");
System.out.println("Review what you entered and try again!");
System.out.println();
}
}
}