0
変数n
に配列のサイズを入力させてから、文字列の1次元配列を作成し、それに文章を埋め込ませる必要があります。私はまた、各文が完全な停止(.
)で終わることを確認する必要があります。ので、ここで私が持っているものです:入力した後スキャナの関数.nextLine()はforループ内で入力を受け付けませんか?
import java.util.*;
class encryption{
void main(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of sentences to be encrypted");
int n=sc.nextInt();
String[] a=new String[n];
System.out.println("Enter the sentences");
for(int i=0; i<n; i++){
a[i]=sc.nextLine();
while(a[i].charAt(a[i].length()-1)!='.'){
a[i]=sc.nextLine();
}
if(i%2==0){
a[i]=rev(a[i]);
}
else{
a[i]=frwd(a[i]);
}
}
for(int i=0; i<n; i++){
System.out.println(a[i]);
}
}
、私は機能rev(...)
とfrwd(...)
を使用して文字列上で動作し、その後、変更された文字列を出力します。
実行時に、最初のfor
ループa[i]=sc.nextLine();
の最初の行は機能していないようです。それは私に直線のindexOutOfBounds
というエラーを与え、それはループのwhile
にまっすぐ行くという意味です。
どうすれば入力を受け入れることができますか?
あなたは余分 'sc.nextLine()'を消費する最後の改行文字を追加することができます。 [this](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo)の参考になるかもしれません。 –
それを解決した@HardikModha! – JavaPilgrim