[英]Printing the negative number's postion from array list
I have been looking over and over this code and can't seem to figure out the answer. 我一直在看这段代码,似乎无法找出答案。 However, I know it is a simple solution, I just can't find it as of right now... 但是,我知道这是一个简单的解决方案,目前我只是找不到它...
Basicly, what i'm trying to is find the postion to all the negative numbers from the array-list. 基本上,我要尝试的是从数组列表中找到所有负数的位置。 (Positionorder: 0,1,2,3,4,5,6,7,8) Then print which position the negative numbers have. (位置顺序:0,1,2,3,4,5,6,7,8)然后打印负数在哪个位置。 When compiling the code Java tells me what the error is: Java编译代码时会告诉我错误是什么:
Test.java:11: error: bad operand types for binary operator '<=' Test.java:11:错误:二进制运算符'<='的错误操作数类型
while (a<=0) { ^
first type: int[] 第一种类型:int []
second type: int 第二种类型:int
1 error"** 1个错误” **
But how can I resolve the problem, anyone who can guide me in the right direction and also an explanation of why while (a < 0)
is not allowed in the current code. 但是,我该如何解决这个问题呢?任何可以指导我正确方向的人,还可以解释为什么当前代码中不允许while (a < 0)
。 (The task is to do this with while
) (任务是使用while
执行此操作)
String posNegative = 0;
int[] a = {1, 4, 5, -2, -4, 6, 10, 3, -2};
while (a < 0) {
posNegative++;
System.out.println(posNegative);
}
There are several problems with your code. 您的代码有几个问题。 First of all, what you actually want for your problem is as simple as this: 首先,您真正想要解决的问题就是这样简单:
int[] a = {1, 4, 5, -2, -4, 6, 10, 3, -2};
for (int i = 0; i < a.length; i++) {
if (a[i] < 0) {
System.out.println(i);
}
}
Now let's look at your code. 现在,让我们看看您的代码。 If you look at the error message it says that you are trying to compare an int[]
, a, and an int
, 0. An int[]
is a completely different type than int
, so they cannot be compared. 如果你看一下错误信息,它说的是您要比较的int[]
一个和int
,0的int[]
是一个完全不同的类型int
,所以他们不能比拟的。 Read int[]
as "int-array". 将int[]
读取为“ int-array”。
There's also the issue of this line: 还有这条线的问题:
String posNegative = 0;
I'm guessing that what you are thinking is that you need posNegative
to be a String so that you can print it. 我猜想您在想什么,您需要posNegative
作为String以便可以打印它。 That, however, is not necessary. 但是,这不是必需的。 System.out.println()
can take pretty much anything you throw at it. System.out.println()
可以处理您扔给它的几乎所有东西。 When you declare a number, declare it as 声明数字时,将其声明为
int posNegative = 0;
a
is an array. a
是一个数组。 An array cannot be less than zero. 数组不能小于零。
You can iterate through the array either by putting 您可以通过以下方式遍历数组
for (int item : a) {
...
or 要么
for (int i = 0; i < a.length; ++i) {
item = a[i];
....
}
You are confusing the array with its contents. 您正在混淆数组及其内容。 The array itself cannot be a negative integer, because it's an array, not an integer. 数组本身不能为负整数,因为它是数组,而不是整数。 To access the array's contents use a[n] where n is the index of the element you want to access. 要访问数组的内容,请使用a [n],其中n是要访问的元素的索引。
int[] a = {1, 4, 5, -2, -4, 6, 10, 3, -2};
for (int n = 0; n < a.length; n++) {
if (a[n] < 0) {
System.out.println(n);
}
}
a is not an int, it is an array of ints. a不是int,而是一个int数组。 If your intent is to loop over an array, you can use an enhanced for loop: 如果您打算在数组上循环,则可以使用增强的for循环:
for(int i : a)
{
posNegative++;
if(i < 0)
{
System.out.println(posNegative);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.