Problem Statement

Write a program that will accept a series of integers from the user, one per line. It will continue to accept integers from the user until the user inputs 0. If the user inputs a negative number, the program should print "Error! Positive Integers Only" and continue to receive input. Once the user inputs 0, the program should print the sum and average of the positive integers input by the user.

    

Write a program that will accept a series of integers from the user, one per line...

// Load required classes
import java.util.Scanner;
import java.io.File;

public class Example{ public static void main(String[] args) throws Exception{ // Scanner variable Scanner reader;
// If an argument is present, // we are reading from a file // specified in args[0] if(args.length > 0){ reader = new Scanner(new File(args[0])); // If no argument, read from System.in }else{ reader = new Scanner(System.in); }
/* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ } }

Write a program that will accept a series of integers from the user, one per line...


Write a program that will accept a series of integers from the user, one per line...

while(){

}

Write a program that will accept a series of integers from the user, one per line...

while(){

}

Write a program that will accept a series of integers from the user, one per line...

while(){
  int x = reader.nextInt();
}

Write a program that will accept a series of integers from the user, one per line...

while(){
  int x = reader.nextInt();
}

It will continue to accept integers from the user until the user inputs 0

while(x != 0){
  int x = reader.nextInt();
}

It will continue to accept integers from the user until the user inputs 0

int x = 0;
while(x != 0){
  int x = reader.nextInt();
}

It will continue to accept integers from the user until the user inputs 0

int x = 1;
while(x != 0){
  x = reader.nextInt();
}

It will continue to accept integers from the user until the user inputs 0

int x = 1;
while(x != 0){
  x = reader.nextInt();
}

If the user inputs a negative number, the program should print "Error! Positive Integers Only" and continue to receive input

int x = 1;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  //logic here
}

If the user inputs a negative number, the program should print "Error! Positive Integers Only" and continue to receive input

int x = 1;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  //logic here
}

...the program should print the sum and average of the positive integers input by the user.

int x = 1;
int sum = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
}
System.out.println("Sum: " + sum);

...the program should print the sum and average of the positive integers input by the user.

int x = 1;
int sum = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
}
System.out.println("Sum: " + sum);

...the program should print the sum and average of the positive integers input by the user.

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

...the program should print the sum and average of the positive integers input by the user.

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

...the program should print the sum and average of the positive integers input by the user.

Logic Bug!

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 1

sum = 0

count = 0

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 1

sum = 0

count = 0

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 3

sum = 0

count = 0

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 3

sum = 0

count = 0

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 3

sum = 3

count = 1









int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 3

sum = 3

count = 1

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 1

sum = 3

count = 1

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 1

sum = 3

count = 1

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 1

sum = 4

count = 2










int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 1

sum = 4

count = 2

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 0

sum = 4

count = 2

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 0

sum = 4

count = 2

int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 0

sum = 4

count = 3








int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 0

sum = 4

count = 3







int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Variables

x = 0

sum = 4

count = 3


int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  if(x != 0){
    sum += x;
    count++;
  }
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

If-Then


int x = 1;
int sum = 0;
int count = 0;
while(x != 0){
  x = reader.nextInt();
  if(x == 0){
    break;
  }
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }
  sum += x;
  count++;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Break

int x = reader.nextInt();
int sum = 0;
int count = 0;
while(x != 0){
  if(x < 0){
    System.out.println("Error! Positive Integers Only");
  }else{
    sum += x;
    count++;
  }
  x = reader.nextInt();
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

Rearrange

int x = 0;
int sum = 0;
int count = 0;
while(true){
  x = reader.nextInt();
  if(x == 0){
    break;
  }else if(x < 0){
    System.out.println("Error! Positive Integers Only");
    continue;
  }else{
    sum += x;
    count++;
  }
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + (double)sum / count);

If-Else If

Many Different Solutions

Choose one and work through the code, recording variable values on paper to confirm that it performs properly