SORU
27 EKİM 2012, CUMARTESİ


NextLine() atlama sonraki kullandıktan sonra(), nextİnt() veya diğer nextFoo() yöntemleri

Benim ilk yazı burada. Java öğrenmeye başladım ve burada tek başıma bir şeyler yapamam benim ilk sorun.

3* *yöntemler nextInt() ve okuma giriş için nextLine() kullanıyorum. Temel olarak bu gibi görünüyor

System.out.println("enter numerical value");    
int option;
option = input.nextInt ();//read numerical value from input
System.out.println("enter 1st string"); 
String string1 = input.nextLine ();//read first string which is skipped
System.out.println("enter 2nd string");
String string2 = input.nextLine ();//read 2nd string which appeared straight after reading numerical value

ve bu sorun ilk olarak sayısal değer girdikten sonra input.nextLine() atlanır ve ikinci input.nextLine() benim çıkış görünüyor, yürütülür

Enter numerical value
3//this is my input
enter 1st string//here suppose to be stopped and wait for my input but is skipped
enter 2nd string//and this line is executed and wait for my input

Benim "uygulama" ve sorun input.nextInt() kullanarak yatıyor. görünüşe göre test ettim Ben silmek nextLine() gayet iyi çalışıyor, string1 = input.nextLine() string1 = input.nextLine() Her iki yürütülür.

CEVAP
14 AĞUSTOS 2011, Pazar


Sorun ilegiriş.() nextİntkomut sadece int değerini okur. Giriş ile okumaya devam etmek.() nextLine "\n" tuşuna basın. aldığınız Bu atlamak için o yüzden eklemek zorundagiriş.() nextLine. Bu açık olmalı umut.

Böyle dene:

   System.out.print("Insert a number: ");
   int number = input.nextInt();
   input.nextLine(); // This line you have to add (It consumes the \n character)
   System.out.print("Text1: ");
   String text1 = input.nextLine();
   System.out.print("Text2: ");
   String text2 = input.nextLine();

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • AndroidSale

    AndroidSale

    17 NİSAN 2011
  • Art Food Kitty - Kelly Eddington

    Art Food Kit

    7 Kasım 2006
  • echifitness

    echifitness

    9 Kasım 2008