2016-03-15 68 views
2

我需要幫助創建一個數組,計數達到給定的數字。輸出應該是這個樣子:創建一個數組,計數達到給定數

Enter a positive integer: 8 
Counting up: 1 2 3 4 5 6 7 8 
Counting down: 8 7 6 5 4 3 2 1 
The first 8 multiples of 5: 5 10 15 20 25 30 35 40 
The first 8 multiples of 10: 10 20 30 40 50 60 70 80 

這是我到目前爲止有:

Scanner input = new Scanner(System.in); 

    int[] myList = new int[1]; 

    System.out.print("Enter a positive integer: "); 
    promptUser(myList); 

    int[] testArray = { 1, 1, 2, 3, 5, 8, 13 }; 
    System.out.print("Test array: "); 
    printArray(testArray); 

    System.out.print("Counting up: "); 
    int[] countingUp = countUp(n); 
    printArray(countingUp); 
} 

public static void promptUser(int[] a){ 
    Scanner input = new Scanner(System.in); 
    for(int i=0; i<a.length; i++){ 
     a[i] = input.nextInt(); 

    } 
} 

public static void printArray(int[] array){ 
    for(int i=0; i<array.length; i++) 
     System.out.print(array[i]); 

    } 

public static int[] countUp(int n){ 
    for(int i=0; i<n; i++){ 
     int count = 0; 
     while(count<n){ 
      count++; 
     } 
    } 
} 
} 

一切似乎除了叫countingUp最後的方法好嗎工作。

非常感謝!

+0

什麼陣列做一個類'COUNTUP '回來?我根本沒有看到任何'return'語句,這意味着你的程序不應該編譯。 – ajb

+0

編譯器肯定會給你一些錯誤代碼? – John3136

+0

爲什麼你需要一個數組myList來存儲單個數值? –

回答

1
public static int[] countUp(int n){ 
     for(int i=0; i<n; i++){ 
      int count = 0; 
      while(count<n){ 
       count++; 
      } 
     } 
    } 

change this to 

public static int[] countUp(int n){ 
     int [] temp=new int[n]; 
     for(int i=0; i<n; i++){ 
     temp[i]=i+1; 
     } 
    return temp; 
    } 



System.out.print("Counting up: "); 
    int[] countingUp = countUp(n); 
    printArray(countingUp); 

In this line change to 

int[] countingUp = countUp(n); 
for(int i=0;i<countingUp.length;i++){ 
    system.out.println(countingUp[i]+" "); 
} 
0

我們可以通過提取共同邏輯的通過提供的初始消息計數開始,次數來運行,將初始值和增量。喜歡的東西,

private static void count(String msg, int times, int init, int inc) { 
    System.out.print(msg); 
    for (int i = 0; i < times; i++) { 
     System.out.print(' '); 
     System.out.print(init); 
     init += inc; 
    } 
    System.out.println(); 
} 

然後,我們可以用什麼實現的要求,整體像

public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 
    System.out.print("Enter a positive integer: "); 
    System.out.flush(); 
    do { 
     int num = scanner.nextInt(); 
     count("Counting up:", num, 1, 1); 
     count("Counting down:", num, num, -1); 
     count(String.format("The first %d multiples of 5:", num), num, 5, 5); 
     count(String.format("The first %d multiples of 10:", num), num, 10, 10); 
     System.out.print("Enter a positive integer: "); 
     System.out.flush(); 
    } while (scanner.hasNextInt()); 
} 

這將產生給出的8輸入所要求的輸出,然後會提示更多的投入。

0

首先,如果您要嘗試更改動態數組大小,那麼它是不可能在Java中看看@this accepted answer。我建議您改用ARRAYLIST

儘管我在代碼中發現了以下錯誤。在你的代碼中,我不明白兩件事。

第一招:

System.out.print("Counting up: "); int[] countingUp = countUp(n); printArray(countingUp);

什麼是n的值?我認爲它沒有被初始化。

第二個:

public static int[] countUp(int n){ for(int i=0; i<n; i++){ int count = 0; while(count<n){ count++; } } }

什麼會回到這個功能呢?你還沒有從中返回任何東西。

0

顯然你不需要一個數組只需按照下面的步驟。

首先創建其處理所有的計算和計數

class SupportCounting { 

public void countUp(int num) { 
    System.out.print("Counting up : "); 
    for (int i = 1; i <= num; i++) { 
     System.out.print(i); 
     System.out.print(" "); 
    } 
    System.out.println(""); 
} 

public void countDown(int num) { 
    System.out.print("Counting Down : "); 
    for (int i = num; i > 0; i--) { 
     System.out.print(i); 
     System.out.print(" "); 
    } 
    System.out.println(""); 
} 

public void printMultiple(int num, int scalefactor) { 
    System.out.print("First " + num + " multiples of " + scalefactor + " : "); 
    for (int i = 1; i <= num; i++) { 
     System.out.print(i * scalefactor); 
     System.out.print(" "); 
    } 
    System.out.println(""); 
}} 

然後使用這個類在你的主要方法

public class Counting { 

public static void main(String[] args) { 

    Scanner reader = new Scanner(System.in); 
    System.out.print("Enter a positive integer : "); 
    int n = reader.nextInt(); 

    SupportCounting sc = new SupportCounting(); 
    sc.countUp(n); 
    sc.countDown(n); 
    sc.printMultiple(n, 5); 
    sc.printMultiple(n, 10); 
}} 

希望幫助

相關問題