【发布时间】:2020-10-17 17:49:01
【问题描述】:
我目前正在制作一个程序,它将用户输入添加到一个名为 score 的 arrayList 中,当输入一个负数时,它会从 while 循环中中断。
我目前正在尝试代码,但是我目前在 else-if 语句中遇到布尔运算符的问题。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Lab03_Part_1
{
public static void main(String[] args)
{
double sum;
double avg;
int i;
boolean acceptableScore = true;
List<Integer> score = new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
while (acceptableScore)
{
if ((score >= 0) && (score <= 100))
{
System.out.println("Enter Student Score: ");
score.add(scanner.nextInt());
}
else if (score > 100)
{
System.out.println("that is not an acceptable Score");
}
else
{
break;
}
}
}
}
这是我得到的当前错误。
The operator >= is undefined for the argument type(s) List<Integer>, int
The operator <= is undefined for the argument type(s) List<Integer>, int
The operator > is undefined for the argument type(s) List<Integer>, int
根据我对此错误消息的理解,主要问题是我在创建 ArrayList 时使用了 Integer,但是 boolean 需要 int?我对编码很陌生,所以这个问题对我来说是一个新领域。
我不是在找人为我编写代码,但我非常感谢有人能提供任何帮助,谢谢。
【问题讨论】:
-
score是一个整数列表,您不能将任何值与列表进行比较。你可以做 listscore.get(index) >= 0表示列表元素
标签: loops arraylist while-loop