分析未知数组大小的数组
Analyzing arrays without known array size
我一直在四处寻找答案,我总能找到能部分回答我的问题的东西。所以,这是我现在的代码:
import java.util.Scanner;
public class VelikaDN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[100];
for (int i = 0; i < array.length; i++) {
array[i] = input.nextInt();
// Number of elements
int counter = 0;
for (int j = 0; j < array.length; j++) {
if (array[j] != 0)
counter++;
}
System.out.println("This array has " + counter + " numbers.");
}
}
}
它有效,但最后我注意到一些事情并不像我想象的那么小。这是输出:http://i.imgur.com/3mmEpUb.png
我试图在整个代码中重新定位打印,试图以某种方式停止循环,但我失败了。我真的不知道是什么问题了。我试图放弃这个问题并完成另一项任务,但正如我已经说过的,循环之外的任何东西都没有出现。
很抱歉,如果这让您感到困惑,我是 Java 的新人,而且我也很不擅长解释。如果您有一些技巧或替代解决方案,请随时将其扔在这里。如果还有什么需要解释的,尽管说吧。
将int counter=0;
放在循环外然后post循环后system.out
我看不出使用 2 个嵌套 for 循环(或 2 个单独的循环)有什么意义,它只会使算法复杂度为 O(n^2)
您可以检查第一个循环中的值是否为 0,以及它是否不增加计数器的值
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[2];
int counter = 0;
for (int i = 0; i < array.length; i++) {
array[i] = input.nextInt();
if(array[i] != 0) {
counter++;
}
}
System.out.println("This array has " + counter + " numbers.");
}
编辑
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[100];
int i = 0;
for (; i < array.length; i++) {
int inputValue = input.nextInt();
if(inputValue <= 0) {
break;
} else {
array[i] = inputValue;
}
}
System.out.println("This array has " + i + " numbers.");
}
编辑 2(信用@Andreas)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[100];
int i = 0;
while (input.hasNextInt()) {
int inputValue = input.nextInt();
array[i++] = inputValue;
}
System.out.println("This array has " + i + " numbers.");
}
我一直在四处寻找答案,我总能找到能部分回答我的问题的东西。所以,这是我现在的代码:
import java.util.Scanner;
public class VelikaDN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[100];
for (int i = 0; i < array.length; i++) {
array[i] = input.nextInt();
// Number of elements
int counter = 0;
for (int j = 0; j < array.length; j++) {
if (array[j] != 0)
counter++;
}
System.out.println("This array has " + counter + " numbers.");
}
}
}
它有效,但最后我注意到一些事情并不像我想象的那么小。这是输出:http://i.imgur.com/3mmEpUb.png 我试图在整个代码中重新定位打印,试图以某种方式停止循环,但我失败了。我真的不知道是什么问题了。我试图放弃这个问题并完成另一项任务,但正如我已经说过的,循环之外的任何东西都没有出现。
很抱歉,如果这让您感到困惑,我是 Java 的新人,而且我也很不擅长解释。如果您有一些技巧或替代解决方案,请随时将其扔在这里。如果还有什么需要解释的,尽管说吧。
将int counter=0;
放在循环外然后post循环后system.out
我看不出使用 2 个嵌套 for 循环(或 2 个单独的循环)有什么意义,它只会使算法复杂度为 O(n^2) 您可以检查第一个循环中的值是否为 0,以及它是否不增加计数器的值
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[2];
int counter = 0;
for (int i = 0; i < array.length; i++) {
array[i] = input.nextInt();
if(array[i] != 0) {
counter++;
}
}
System.out.println("This array has " + counter + " numbers.");
}
编辑
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[100];
int i = 0;
for (; i < array.length; i++) {
int inputValue = input.nextInt();
if(inputValue <= 0) {
break;
} else {
array[i] = inputValue;
}
}
System.out.println("This array has " + i + " numbers.");
}
编辑 2(信用@Andreas)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers: ");
int[] array = new int[100];
int i = 0;
while (input.hasNextInt()) {
int inputValue = input.nextInt();
array[i++] = inputValue;
}
System.out.println("This array has " + i + " numbers.");
}