So the prompt is here:
Assignment #2 will be the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the inputted numbers, and outputs the results of those calculations to standard output. The numbers could be delimited by any kind of whitespace, i.e. tabs, spaces, and lines (Note that if you use the Scanner class, you do not have to worry about these delimiters. They will be taken care of). Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:
The maximum integer is 0
The sum of the positive integers is 0
The sum of the odd integers is 0
The count of even integers in the sequence is 1
This means that using all numbers your program reads (including the last number 0), you need to compute the maximum, the sum of only positive integers (integers that are greater than 0), the sum of odd integers (integers that cannot be divided by 2, you can use "num%2 != 0"), and count how many even integers (integers that can be divided by 2) in the sequence.
Note that the above is an output for the first test case. For other test cases, you will have different numbers.
Do not output a prompt to query for the numbers. The number 0 is included in the sequence of integers and should be included in all of your calculations.
My Code:
import java.util.Scanner; // uses the Scanner package located in the "java.util" directory
public class Assignment2
{
public static void main (String[] args)
{
int number; // declaring an integer: number
Scanner console = new Scanner(System.in); // creates a new scanner
max_int = -1; // so the first int will be larger than this
even_count = 0; // 0 because we don't have any yet
while((num = console.nextInt()) != 0) {
do // performs a do loop that executes several if statements to see if values meet the criteria
{
if(num > max_int) // This calculates whether or not the integer is the greatest amount
{
max_int = num; // sets max integer equal to the number
}
if(num > 0) // Adds the number to the sum if the integer is positive.
{
pos_sum = pos_sum + num;
}
if (num % 2 != 0) // Adds the integer to the sum of odds if the number is odd
{
odd_sum = odd_sum + num;
}
if (num % 2 == 0) // This counts the number of even numbers
{
even_count++;
}
}
System.out.print("The maximum integer is" + max_int + "\n"
+ "The sum of the positive integers is" + pos_sum + "\n"
+ "The sum of the odd integers is" + odd_sum + "\n"
+ "The count of even integers in the sequence is" + even_count + "\n");
}
}
I keep getting errors about parsing and the code expecting something and even some initializations. I've taken Java last semester, but after summer I kind of forgot all the syntax. I'm really have some big troubles with this, already spent 2 hours doing this and i'm getting migrains. If anyone can help correct me code, it'll be high appreciated. I don't even know if I'm doing it right, but I've tried unlike some people who just post their questions hoping someone can do all their hw. I'm here to learn and see what I did wrong.