2016-10-28 57 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(); 
     } 
相關問題