logo

Skeneris un nextChar () Java

Skenera klase Java valodā atbalsta nextInt(), nextLong(), nextDouble() utt. Bet nav nextChar() (sk. šis piemēriem) Lai lasītu zīmi, mēs izmantojam next().charAt(0) . Funkcija next() atgriež nākamo marķieri/vārdu ievadē kā virkni un funkcija charAt(0) atgriež pirmo rakstzīmi šajā virknē, skaitli 0 funkcijā CharAt( NUMBER ) apzīmē ievadītās virknes atsevišķa vārda indeksu un iestatiet šo indeksa rakstzīmi uz char mainīgo.

Java








// Java program to read character using Scanner> // class> import> java.util.Scanner;> public> class> ScannerDemo1 {> >public> static> void> main(String [] args){> >Scanner sc =>new> Scanner(System.in);> >char> c = sc.next().charAt(>1>);> >// here this is the indexing of the element to take //> >System.out.println(>'c = '>+c);> >}> > }> > > >



>

>

Ievade:

ge>

Izvade:

c = e>