即时在Java初学者,我有一个问题要做到: 问题提示用户5次进入,股票的名称,然后股价随后的数股份拥有,我们应该计算所有股票价值的总和,我只用两个提示使用循环编写它,但我的问题是,在第二个提示时间循环跳过字符串输入的第二个股票名称,而不是提升浙江...波纹管是代码:JAVA初学者帮助需要在一个循环中字符串输入
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
什么使得输入在第二个循环中跳过?
它可能是由于你的需要来处理线令牌年底获得nextInt()或nextDouble()之后。考虑放入下一行() –
请为代码块使用一致的逻辑缩进。这样做会使代码流更易于理解。这对于像条件语句或循环语句这样的事情尤其重要。 –