2013-04-06 3 views
0
public static void main(String[] args) {  
    Scanner in = new Scanner(System.in); 
    System.out.print("Please choose.('e' to encrypt, 'd' to decrypt, 'q' to quit): "); 
    String userIn= in.next(); 
    if(userIn.equals("e")){ 
     System.out.println("Please enter your text that you want to encrypt: "); 
     String userInput = in.nextLine(); 

     System.out.print("Please enter your shift key(0-25): "); 
     int userS = in.nextInt(); 
     if(userS < 0 || userS > 25){ 
      System.out.print("Invalid shift key, please enter a valid shift key: "); 
      userS = in.nextInt(); 
     } 

В моей вышеизложенной программы в следующей части кода:Авто пропуск входные данные

System.out.println("Please enter your text that you want to encrypt: "); 
       String userInput = in.nextLine(); 

       System.out.print("Please enter your shift key(0-25): "); 

Он пропускает этот userInput, он идет по ней и просит для смены ключа, прежде чем я ввожу текст.

ответ

1

Это установил его (протестировано в Eclipse):

Scanner in = new Scanner(System.in); 
System.out.print("Please choose.('e' to encrypt, 'd' to decrypt, 'q' to quit): "); 
String userInput = in.nextLine(); 
if (userInput.equals("e")) 
{ 
    System.out.println("Please enter your text that you want to encrypt: "); 
    userInput = in.nextLine(); 

    System.out.print("Please enter your shift key(0-25): "); 
    int userS = Integer.parseInt(in.nextLine()); 
    if (userS < 0 || userS > 25) 
    { 
     System.out.print("Invalid shift key, please enter a valid shift key: "); 
     userS = Integer.parseInt(in.nextLine()); 
    } 
} 
in.close(); 

Я изменил вашу userIn переменную просто userInput, так как мы Бесполезный ему это нужно; ваш звонок next() также был изменен на nextLine().

Я также сменил все ваши nextInt() на nextLine(). Это поможет вам избежать Exception.

И наконец, всегда закрывайте Scanner, когда вы закончите с этим, чтобы сохранить системные ресурсы.

0

Изменение:

String userInput = in.nextLine() 

в

in.nextLine(); String userInput = in.nextLine(); 
0

Простой, изменить ваш код для

String userInput = in.next(); 
+1

Если вы сделаете это, то вы не можете ввести предложение. Вы можете ввести только слово. – user2059140

Смежные вопросы