Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm learning for loops and arrays. I'm supposed to program a small problem but not sure how to approach it at all. Here it is.

Link To Problem

What I have tried:

I've tried this but I'm not sure it's correct.

   		if (var1 == var2 || var1 == var3 || var1 == var4 || var1 == var5 ) {
   			
   			System.out.println("You entered an existing number.");
   			int revar1 = sc.nextInt();

}
Posted
Updated 17-Nov-20 21:28pm
Comments
The Other John Ingram 17-Nov-20 16:33pm    
reread the problem
there is supposed to be loops and arrays in the program
you have none

No, I'd say it's wrong.
The question asks you to "input 5 numbers" and "ignore any that have been entered before".
What if I enter 1, 2, 2? You should be ignoring them as they are entered, and checking against the "current list" of values.

To do that, create an array of 5 integers, and an index which starts at zero.
Now use a loop which continues until you index equals 5 - the number of integers you are required to collect.
Inside the loop, get the user to input a number.
Use a for loop to check it against the ones he entered before (they are in the array, with values less than the index you created). If the value is there already, ignore it.
Otherwise, insert it to the array, and increment the index.

Try it manually, and you'll see what I mean - it's the same process you would use to do it on paper.

If you can't get started, have a look at this: How to Write Code to Solve a Problem, A Beginner's Guide[^] - it should give you a technique for working things out a bit better.
 
Share this answer
 
Comments
CPallini 18-Nov-20 2:06am    
5.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900