- #1
iamjon.smith
- 117
- 3
Simple Array assignment, directions as follows:
(Must be implemented using a one-dimensional array.)
Write an application that inputs ten numbers from the user, each number can be between 10 and 100, inclusive. As each number is read in determine if it is a number already entered. If it is a duplicate move on to the next number, if it is unique store the number in the array. After all ten numbers have been entered display the complete set of unique numbers that were entered
My problem:
I have created an array that accepts user input for all 10 array elements, it validates the integers to ensure they are between 10 -100, and outputs them to the screen correctly. The problem I am having is when the user inputs a duplicate number. The duplicate number needs to be dropped (preferably with an error message requesting new input) and unique numbers accepted to the array. It must output 10 unique numbers.
my code:
(Must be implemented using a one-dimensional array.)
Write an application that inputs ten numbers from the user, each number can be between 10 and 100, inclusive. As each number is read in determine if it is a number already entered. If it is a duplicate move on to the next number, if it is unique store the number in the array. After all ten numbers have been entered display the complete set of unique numbers that were entered
My problem:
I have created an array that accepts user input for all 10 array elements, it validates the integers to ensure they are between 10 -100, and outputs them to the screen correctly. The problem I am having is when the user inputs a duplicate number. The duplicate number needs to be dropped (preferably with an error message requesting new input) and unique numbers accepted to the array. It must output 10 unique numbers.
my code:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myarray;
/**
*
* @author Jon and Jessica
*/
import java.util.Scanner;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Declare variables
int newNumber;
int myNumber;
Scanner input = new Scanner( System.in ); // create Scanner object
int[] myArray = new int[10]; // Create an array with an index of 10
// Allow user to set all numbers in array
for (int number = 0; number < myArray.length; number++){ // Initialize counter
System.out.println("Please enter a number between 10 - 100: ");
// Ask until user enters a unique number
do{
newNumber = input.nextInt();
// User friendly error message
if ( newNumber < 10 || newNumber > 100){ // Check to make sure user input valid number
System.out.println("The Number must be between 10 - 100! Let's try that again: "); // Request new number if first is negative
}
} while ( newNumber < 10 || newNumber > 100);
myArray[number] = newNumber;
}
// Print all array elements
for(int number = 0; number < myArray.length; number++){
System.out.printf("The Number entered at %s is %s\n",number, myArray[number]);
}
}
}