Hi everyone, I'm pretty new to programming and I am currently taking a beginner's programming class. I had an assignment to calculate the the number of broadsheets needed for a certain amount of exam pages as well as finding the average of 5 inputted test scores. So I finally ended up using an array to find my average. The program is suppose to enforce the rule that
if (i < 0) ---> i = 0
if (i > 100) ---> i = 100
The assignment isn't for any points but rather for practice. It's been bothering me since it won't enforce the rule no matter what I change. Well here's my original code for the whole program (the problem is in the bottom half, although it doesn't have any if/else statements):
import java.io.*; public class HW4X { public static void main(String args[]) throws IOException { BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in)); int e, b, s1, s2, s3, s4, s5, t; System.out.print("Enter the amount of exam pages: "); e = Integer.parseInt(keybd.readLine()); if (e == 0) { System.out.println("Please enter a valid amount!"); } else if ((e % 4) == 0) { b = e / 4; System.out.println("You will need " + b + " broadsheet(s)."); } else if ((e % 4) >= 1) { b = (e / 4) + 1; System.out.println("You will need " + b + " broadsheet(s)."); } /* This is the part that is suppose to calculate the average as well as the part I will be substituting */ System.out.println("Enter test scores: "); s1 = Integer.parseInt(keybd.readLine()); s2 = Integer.parseInt(keybd.readLine()); s3 = Integer.parseInt(keybd.readLine()); s4 = Integer.parseInt(keybd.readLine()); s5 = Integer.parseInt(keybd.readLine()); t = (s1 + s2 + s3 + s4 + s5) / 5; System.out.println("The average is: " + t); } }
And here's the part with the array average (I will be substituting the array into the previous program) with the if/else if statements that aren't working:
import java.io.*; public class x { public static void main(String args[]) throws IOException { BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number: "); int nums[] = new int[5]; int result=0; int i=0; for(i=0; i < nums.length; i++) { nums[i] = Integer.parseInt(keybd.readLine()); result = result + nums[i]; if (nums[i] < 0) { nums[i] = 0; } else if (nums[i] > 100) { nums[i] = 100; } } System.out.println("Average is = " + result/nums.length); } }
Any help is appreciated!