Note that there are some explanatory texts on larger screens.

plurals
  1. POTaking User Input for an Array
    text
    copied!<p>A link to the assignment: <a href="http://i.imgur.com/fc86hG9.png" rel="nofollow">http://i.imgur.com/fc86hG9.png</a></p> <p>I'm having a bit of trouble discerning how to take a series of numbers and apply them to an array without a loop. Not only that, but I'm having a bit of trouble comparing them. What I have written so far is:</p> <pre><code>import java.util.Scanner; public class Lottery { public static void main(String[] args) { int userInputs[] = new int[5]; int lotteryNumbers [] = new int[5]; int matchedNumbers =0; char repeatLottery = '\0'; Scanner in = new Scanner (System.in); do{ System.out.println("Enter your 5 single-digit lottery numbers.\n (Use the spacebar to separate digits): "); for(int i = 0; i &lt;5; i++ ) userInputs[i] = in.nextInt(); System.out.println("Your inputs: "); printArray(userInputs); System.out.println("\nLottery Numbers: "); readIn(lotteryNumbers); for(int i=0; i&lt;5; i++) { System.out.print(lotteryNumbers[i] + " "); } matchedNumbers = compareArr(userInputs, lotteryNumbers); System.out.println("\n\nYou matched " + matchedNumbers + " numbers"); System.out.println("\nDo you wish to play again?(Enter Y or N): "); repeatLottery = in.next().charAt(0); } while (repeatLottery == 'Y' || repeatLottery == 'y'); } public static void printArray(int arr[]){ int n = arr.length; for (int i = 0; i &lt; n; i++) { System.out.print(arr[i] + " "); } } public static void readIn(int[] List) { for(int j=0; j&lt;List.length; j++) { List[j] = (int) (Math.random()*10); } } public static int compareArr (int[] list1, int[] list2) { int same = 0; for (int i = 0; i &lt;= list1.length-1; i++) { for(int j = 0; j &lt;= list2.length-1; j++) { if (list1[i] == list2[j]) { same++; } } } return same; } </code></pre> <p>}</p> <p>As you'll notice, I commented out the input line because I'm not quite sure how to handle it. If I have them in an array, I should be able to compare them fairly easily I think. This is our first assignment handling arrays, and I think it seems a bit in-depth for only having one class-period on it; So, please forgive my ignorance. :P</p> <p>Edit: </p> <p>I added a new method at the end to compare the digits, but the problem is it compares them in-general and not from position to position. That seems to be the major issue now. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload