我是Java初学者,我有一个问题要做:问题提示用户输入5次,股票的名称,然后是股价,然后是拥有的股票数量,我们应该计算所有股票价值的总和,我使用循环仅使用两个提示编写它,但我的问题是,在第二个提示时间,循环跳过第二个股票名称的字符串输入而不是提示...下面是代码:
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
是什么让输入在第二个循环期间跳过?