2016-03-15 67 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); 
}} 

希望帮助

相关问题