Java newbie here just trying to figure out the solution to this problem. The task is to create a program that asks a user the following:
Or, display an error message if the score is not found. The program is to store the data in the array "exams". My code so far is below.How many tests scores? 2 [enter]
Enter test score #1:70 [enter]
Enter test score #2:50 [enter]
Enter a score you wish to search: 50 [enter]
You scored 50 on test #2.
Exam class
ExamTester classpublic class Exam { private int[] exams; public Exam(int[] exams2) { this.exams = exams2; } public void sequentialSearch(int check) { for (int i = 0; i < exams.length; i++) { if (check == exams[i]) System.out.println("You got a " + check + " on exam "+ (i+1)); else System.out.println("The test score was not found."); } } }
The program works, but it always outputs "The test score was not found" even after saying "You got x on exam y". I know it has something to do with that for loop, but I am stuck on how to fix it. Any help would be GREATLY appreciated!import java.util.Scanner; public class ExamTester { public static void main(String[] args) { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in); System.out.println("How many exams?"); int numexams = keyboard.nextInt(); int[] exams = new int[numexams]; for (int a = 0; a < numexams; a++) { System.out.print("Enter a score on test # "+ (a+1) + ": " ); exams[a] = keyboard.nextInt(); } System.out.println("Enter a score that you want to search: "); int check = keyboard.nextInt(); Exam e = new Exam(exams); e.sequentialSearch(check); } }
Thanks!!