2016-10-28 85 views
0

我想有两个子程序createArray()print()Print()将需要multArray变量createArray()我写了程序,以便该数组不是在本地创建的主要。我意识到我可以将createArray设置为createArray(int a, int b)但是我决定反对它。这会回来咬我现在还是还有我的方式来完成这个没有做出建议的变化?有没有办法从createArray()在子程序print()中打印multArray?

import java.util.*; 
import java.io.*; 

public class Array { 

    public static void main(String[] args) { 

     Scanner scan = new Scanner(System.in); 
     String newLine = System.lineSeparator(); 
     String choiceInput; 
     boolean stop = false; 
     boolean firstTime = true; 


     while (stop == false){ 

      if (firstTime == true) { 
       System.out.println("Welcome To The Multiplications Table Creator!" + newLine + newLine + "Would you like to:" + newLine + newLine + "Create   Print   Exit" + newLine + newLine + "Please enter one of the above in the space below: "); 
      } 
      else { 
       System.out.println("Welcome Back!" + newLine + newLine + "Would you like to:" + newLine + newLine + "Create   Print   Exit" + newLine + newLine + "Please enter one of the above in the space below: "); 
      } 
      choiceInput = scan.nextLine().toUpperCase(); 

      if (choiceInput.equals("CREATE")) { 
       createArray(); 
       firstTime = false; 

       for (int count = 0; count < 10; count++) { 
        System.out.println(newLine); 
       } 

      } 
      else if (choiceInput.equals("PRINT")) { 
       print(); 
       firstTime = false; 
      } 
      else if (choiceInput.equals("EXIT")) { 
       for (int count = 0; count < 10; count++) { 
        System.out.println(newLine); 
       } 
       System.out.print("Thank you for using the program!"); 
       for (int count = 0; count < 2; count++) { 
        System.out.println(newLine); 
       } 
       stop = true; 
      } 
      else System.out.println("You did not enter one of the above!"); 
     } 
    } 

    public static int[][] createArray() { 

     Scanner s = new Scanner(System.in); 
     String newLine = System.lineSeparator(); 
     int a; 
     int b; 

     System.out.print("How big would you like your multiplication table to be? (A x B)" + newLine + "A: "); 
     a = s.nextInt(); 
     System.out.println(a + " x "); 
     b = s.nextInt(); 

     int[][] multArray = new int[a][b]; 

     for (int countA = 1; countA <= a; countA++) { 
      for (int countB = 1; countB <= b; countB++) { 
       multArray[countA - 1][countB - 1] = countA * countB; 
      } 

     } 
     System.out.print("Creating ."); 
     delay(1000); 
     System.out.print(" ."); 
     delay(1000); 
     System.out.print(" ."); 
     delay(1000); 
     System.out.println(newLine + "Done."); 
     return multArray; 

    } 

    public static void print() { 
     **//This is where I need to print multArray created above is it possible?** 
    } 

    public static void delay(int millis) { 
     try { 
      Thread.sleep(millis); 
     } 
     catch (InterruptedException exp) { 
     } 
    } 
} 
+1

notenote 1:定义一个全局扫描程序并重用它,不需要定义每个扫描程序的倍数。旁注2:“if(!stop)”和“if(firstTime)”比我眼中的bool值更好。 – SomeJavaGuy

+0

您没有存储函数'createArray()'的结果,因此您无法正确使用'print()'。 您需要将它作为私有成员存储,然后在'print()'中访问它,或者将它作为参数传递给'print()'。 – Ghost93

+0

在这种情况下,'return multArray'是不够的吗?在代码方面,这需要我做什么? –

回答

1

createArray方法返回一个int[][]数组,所以你可以做这样的事情

int[][] multiArray = createArray();//存储在多阵列创建阵列方法

的结果现在改变你的print方法接受一个int[][]阵列,像这样的东西

public static void print(int[][] multiArray);它接受一个int [] []数组作为参数

multiArray //打印方法打印方法当调用打印方法是这样的

print(multiArray)//传递createArray的早期结果是multiArray打印方法。

现在打印方法里面可以打印multiArray

+0

好的谢谢你的帮助,但是在代码中'int [] [] multiArray = createArray();'去哪? –

+0

当前您正在调用createArray方法的地方。它的内部如果条件为'if(choiceInput.equals(“CREATE”))',只需将此行'createArray();'替换为'int [] [] multiArray = createArray();'。 – Ravikumar

+0

非常感谢您的帮助! –

0

你想遍历你的y轴和x轴。这个例子应该打印每列的所有行元素。因此,第一个for-loop迭代遍历行,第二个遍历单行中的每个元素。

public static void printArray(int[][] multArray) { 
    for(int i = 0; i < multArray.lenght(); i++) 
     { 
      for(int j = 0; j < multArray[i].lenght(); j++) 
      { 
      System.out.printf("%5d ", multArray[i][j]); 
      } 
      System.out.println(); 
     } 
相关问题