Dark horse JavaSE video follow-up, set exercises

Enter an integer from 1-100 with the keyboard and add it to the collection until the sum of all data in the collection exceeds 200

 

import java.util.ArrayList;

import java.util.Scanner;

public class ArrayListP {

    public static void main(String[] args) throws Exception {

         ArrayList<Integer> arr=new ArrayList<>();

        Scanner sc= new Scanner(System.in);

        int sum=0;

        while(sum<=200){

            String str=sc.nextLine();

            int num=Integer.parseInt(str);

            if(num>=1&&num<=100){

                arr.add(num);

                am=am+am;

            }else{

                System.out.println("Please re-enter");

                continue;

            }

        }

        System.out.println(arr);

    }

}

Guess you like

Origin blog.csdn.net/lina_lee1/article/details/131200128