在while循环中输入负数时,停止接收Arraylist的用户输入

时间:2020-06-27 05:41:51

标签: loops arraylist while-loop

我目前正在编写一个程序,将用户输入添加到一个名为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?我对编码非常陌生,所以这个问题对我来说是新领域。

我不是要找人为我编写代码,但我真的很感谢别人可以提供的任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

问题

您直接将用户输入添加到ArrayList。

您应该做什么:

将输入存储在变量中,对其进行检查并采取相应措施。

  public status: any = {
    PRG: "In Progress",
    COMP: "Completed",
    FAIL: "Failed",
  };