Java,

2016-04-09 9 views
2

dizgisini yok sayar Programlamada yeniyim ve bu problem her zaman bana karşı geliyor Program çalıştırdığımda Java, eğer bir içinde bir string girişi yoksayar. Ne yaptım?Java,

import java.util.Scanner; 

public class JavaApplication { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     System.out.print("------ FEEDBACK/COMPLAINT ------\n" 
       + "-------------------------------------\n" 
       + "| 1: Submit Feedback |\n" 
       + "| 2: Submit Complaint |\n" 
       + "| 3: Previous Menu |\n" 
       + "-----------------------------------\n" 
       + "> Please enter the choice: "); 
     int feedorcomw = input.nextInt(); 

     if (feedorcomw == 1) { 
      String name; 
      System.out.print("> Enter your name (first and last): "); 
      name = input.nextLine(); 
      System.out.println(""); 
      System.out.print("> Enter your mobile (##-###-####): "); 
      int num = input.nextInt(); 

     } 

    } 
} 
+0

Ne tür bir hata alıyorsunuz? – Andrew

+2

Belki de bu sizin sorununuza cevap veriyor http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – RubioRic

+0

@and Düzenleme bilgilerinizi fazla anlamıyor - neden Biçimlendirmeyi düzeltmek yerine, en az bir örnek oluşturabilir misiniz? – Flexo

cevap

1

Eğer

nextLine Tarayıcı # nextInt yöntem girişinin son satır karakteri tüketmez ve dolayısıyla bu satır Tarayıcı # yanındaki çağrısında tüketilmektedir gerçeğini ommitting edilir Ondan sonra bir input.nextLine(); eklemeyi deneyin ve her şey iyi

Örnek çalışacaktır:

public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 

    System.out.print("------ FEEDBACK/COMPLAINT ------\n" 
      + "-------------------------------------\n" 
      + "| 1: Submit Feedback |\n" 
      + "| 2: Submit Complaint |\n" 
      + "| 3: Previous Menu |\n" 
      + "-----------------------------------\n" 
      + "> Please enter the choice: "); 
    int feedorcomw = input.nextInt(); 

    input.nextLine(); 
    if (feedorcomw == 1) { 
     String name; 
     System.out.print("> Enter your name (first and last): "); 
     name = input.nextLine(); 
     System.out.println(""); 
     System.out.print("> Enter your mobile (##-###-####): "); 
     int num = input.nextInt(); 

    } 

}