We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11
import java.util.
Arrays; import java.util.Scanner;
/** * * @author student */ public class Sy_FinalsSummative_ve rAdditionalPoint {
public static void
main(String[] args) { int [] array = new int [10]; //Array Number Generator for (int i = 0; i<array.length; i+ +) { array[i] = (int) (Math.random () * 20) + 1; } //Display elements of the array System.out.println("Array elements are"); System.out.println(Arrays .toString(array)); //Inputs from user (Find, replace, replace first or all elements) System.out.println("Find an element: "); Scanner console = new Scanner(System.in); int findElement = console.nextInt(); System.out.println("Repl ace an element: "); int replaceElement = console.nextInt(); System.out.println("Repl ace the first[F/f] element found or all[A/ a]elements found?"); String whatElement = console.next().toLowerC ase(); //Processes user input switch (whatElement){ case "f" -> { for (int i=0; i<array.length; i++){ if (array[i] == findElement){ array[i]=replaceElement; break;} } } case "a" -> { for (int i=0; i<array.length; i++){ if (array[i] == findElement ) array[i]=replaceElement;} break;} default -> System.out.println("Invali d input!"); } System.out.print("New elements are:"+Arrays.toString(arr ay)); } }