what you want from us boyutta olusturmamizi dizi ve bu dizi bi icerisinde aratmamizi istiyor Deger, Deger aratirken dizideki verilerin artan Sirad olmasini istiyor eger siralama artan degilse Istisna olusmasini you Deger bulmadan cikmasini istiyor, eger Deger bulunamazsa Yine bir Istisna olusturmamizi istiyor.
===========
Searching a vector
Write a function that searches an integer value in a vector of integers, or-ordinated
in ascending order. Both the value and the carrier are subjects of
function. The function returns the position of value in the vector, but lan-
`cia
an exception if it detects that the carrier passatole argument is not
`
ordered or if the value is not present in the vector. Then write a pro-
main program that reads from a keyboard and a vector value and calls the search function
, catch any exceptions.
=========================================
Cozum:
import IngressoUscita.Console;
public class Search {
/ / static asagidaki Dahila sinifimizi olarak belirliyoruz cunku static yapmazsak main bolumunde calistiramayiz ve bir haber Istisna firlatabilecegini veriyorus throws Exception ifadesi ile.
public static int ricerca (int val, int [] yes) throws Exception {
/ / Found that taking the value of a boolean variable to false.
boolean trovato = false;
int i = 0;
/ / the while that there are checking whether the value before the first cycle and i would like to control the number and length of the finely olmadigni are looking for value in the array i'ninci average value is small and equal olmadigni are checking.
while (! Trovato & & (i \u0026lt;vet.length) & & (vet [i] \u0026lt;= val)) {
/ / Here, bigger than 0, and if I check the directory are ascending towards the siralnip siralanmadigni
if (i> 0 & &! (vet [i]> = yes [i - 1])) {
/ / This is the idea of \u200b\u200bjeans with the exception of us can return an exception Create the
throw new Exception ("Non ordinato");}
/ / the value of the loop are going observed to
if (yes [i] == val) trovato = true;
else i + +;}
/ / Create the
exception here again if (! trovato) throw new Exception ("non e travato");
else return i;}
/ / public static void main section
writing main (String [] args) {
Console.scriviStringa ("Dimensione di array ");
Console.leggiIntero int n = ();
/ / user is the size of the array and would like to fill the array below.
int [] vettore = new int [n];
for (int i = 0; i \u0026lt;n, i + +) {
Console.scriviStringa ("insersci" + i);
vettore [i] = Console.leggiIntero ();
} / / user wants to search the value ask
Console.scriviStringa ("Valore di cercare ???");
int val = Console.leggiIntero ();
/ / Here an exception to the above are formed in the classroom are formed below the call oldugmuz ricerca try and catch the exception within exception where you catch the exception thrown from above between the user report the situation. Try {int
ricerca posizione = (val, vettore);
Console.scriviStringa ("Valore =" + posizione);
} catch (Exception e) {Console.scriviStringa ("ha trovato Excepion: "+ e.getMessage ());}
}}
0 comments:
Post a Comment