java - 循环后,扫描仪在调用时未注册 scan.nextLine()?
问题描述
这是整个代码,它基本上是读取输入并要求一个整数,直到给出。但我似乎无法理解为什么在我的循环之后,扫描仪没有注册对 rotate = scan.nextLine(); 的调用?
import java.util.Scanner;
public class PJ{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
String phrase;
String phraseMut;
int index =0 ;
boolean bool = false;
String rotate;
System.out.println("Enter your word or phrase: ");
phrase = scan.nextLine();
phraseMut = phrase;
System.out.println();
do {
System.out.println("Enter an integer: ");
//scan.nextLine();
if (scan.hasNextInt()) {
index = scan.nextInt();
bool = true;
}/*if (index > phraseMut.length()) {
System.out.println("Index is out of bounds.");
System.out.println("Please enter an integer value between 0 and
"+phraseMut.length());
}*/else {
bool = false;
System.out.println("Error: Index is not Integer.");
scan.nextLine();
}
}while (!bool);
System.out.println();
System.out.println("Rotating phrase to bring index "+index+" to the front. . .");
int count =0;
for(int i = 0; i < index; i++){
phraseMut = phraseMut.substring(1,phrase.length())+""+phraseMut.substring(0,1);
count++;
System.out.println(phraseMut);
}
System.out.println();
System.out.println("Do you want to rotate again? (yes/no)");
rotate = scan.nextLine();
rotate = rotate.toLowerCase();
double placeholder = 0.0; //will be average shifts per rotation
if( rotate.equals ("no")){
System.out.println("\""+phrase+"\" rotated "+count+
" times, final result \""+phraseMut+"\"");
System.out.println("Average shifts per rotation: "+placeholder);
}
if( rotate.equals("yes")){
System.out.println("Enter the index of a character in your phrase: ");
index = scan.nextInt();
scan.nextLine();
System.out.println();
System.out.println("Rotating phrase to bring index "+index+" to the front. . .");
for(int i = 0; i < index; i++){
phraseMut = phraseMut.substring(1,phrase.length())+""+phraseMut.substring(0,1);
count++;
System.out.println(phraseMut);
}
}
System.out.println();
}
}
特别是代码块:
System.out.println("Do you want to rotate again? (yes/no)");
rotate = scan.nextLine();
rotate = rotate.toLowerCase();
是不扫描,我不明白为什么?我可能在这里遗漏了一些非常细微的东西,但我想补充一点,我对代码也很陌生,这特别给我带来了麻烦。任何输入表示赞赏。