Question
Use a one-dimensional array to solve the following program. Write a application that inputs six numbers [Keep the numbers in an array]. The numbers must
Use a one-dimensional array to solve the following program. Write a application that inputs six numbers [Keep the numbers in an array]. The numbers must be between 20 and 200, inclusive. If the number is not between 20 and 200, ask for another input. When a number is entered, display the number only if it is not a duplicate of a number already entered. If it is a duplicate of a number already entered, display a message that the number is a duplicate number and ask for another input. Provide for the worst case in which all six numbers are different. Display the complete set of unique values input after the user enters each new value. UPLOAD Duplicate.java
I typed in the following code. Is this correct?
// Duplicate.java
// Importing packages
import java.util.Scanner;
public class Duplicate {
public static void main(String[] args) {
int counter[] = new int[201]; // placing the number limit
int arr[] = new int[6]; // indicating the user is inputtinng 6 numbers
int n, count = 0; // count of number being input
// create Scanner for input
Scanner sc = new Scanner(System.in);
// number must be between 20-200
while(true) {
System.out.println("Enter number between 20-200:");
// Enter the the next number
n = sc.nextInt();
// If number is not between 20-200
if(n<20 || n>20) {
System.out.println("Number must be between 20-200. Try again");
continue;
}
// using the while counter controlled loop if number is between 20-200
if(counter[n]==0) {
counter[n]=1;
arr[count++]=n;
}
// If number is a duplicate
else {
System.out.println("Number already exists. Try again");
continue;
}
// Break the statement if six integers are inputted
printArray(arr, count);
if(count==6);
break;
}
}
// Print arrays
private static void printArray(int[] arr, int n) {
for(int i=0; i System.out.print(arr[i]+ ""); System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started