88.7k views
5 votes
This is for AP Computer Science and I need help answering this question. If anyone could help it would be very helpful.

This is for AP Computer Science and I need help answering this question. If anyone-example-1
User Deeko
by
5.1k points

1 Answer

7 votes

import java.util.Scanner;

public class PosNums {

public static void main(String[] args) {

System.out.println("Enter positive numbers (-1 to stop)");

Scanner myObj = new Scanner(System.in);

int num = myObj.nextInt();

int total = 0;

while (num!=-1){

if (num < 0 && num!= -1){

System.out.println("Only positive numbers will be counted toward your total.");

}

else if (num >=0){

total += num;

}

num = myObj.nextInt();

}

System.out.println("Sum is " + total);

}

}

If you have any other questions, I'll do my best to help. Best of luck.

User Keneisha
by
4.5k points