私は Java の初心者ですが、やるべき問題があります。この問題により、ユーザーは 5 回入力するように求められます。株の名前、株価、所有する株数、そしてすべての株の値の合計を計算する必要があります。 、私はループを使用して2つのプロンプトのみを使用してそれを書きましたが、私の問題は、2番目のプロンプトでループがプロンプトの代わりに在庫の2番目の名前の文字列入力をスキップすることです...以下はコードです:
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double sharePrice =0,stockPrice = 0, temp = 0 ;
int i = 0;
double sum=0;
String name;
while (i < 2) {
System.out.println("Enter the Name of the Stock ");
name = input.nextLine();
System.out.println("Enter the Share price ");
sharePrice = input.nextDouble();
System.out.println("Enter the number of owned shares");
int numberOfshares = input.nextInt();
stockPrice = sharePrice * (double)(numberOfshares);
sum += stockPrice;
i++;
}
System.out.println("the total stockprice owned is: " + sum );
}
}
そして、これは私が得る出力です:
Enter the Name of the Stock
nestle
Enter the Share price
2
Enter the number of owned shares
4
Enter the Name of the Stock
Enter the Share price
2 番目のループで入力がスキップされる原因は何ですか?