1

我是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

是什么让输入在第二个循环期间跳过?

4

3 回答 3

3

nextInt()再次根据我的评论,问题是您的代码在调用or时不处理行尾(EOL)令牌nextDouble()

解决方案是input.nextLine()在获取您的 int 和 double 后使用以吞下 EOL 令牌:

sharePrice = input.nextDouble();
input.nextLine();  // add this 

System.out.println("Enter the number of owned shares");
int numberOfshares = input.nextInt();
input.nextLine();  // add this 
stockPrice = sharePrice * (double)(numberOfshares);
于 2012-08-25T14:47:50.673 回答
3

问题是你最后一次打电话

int numberOfshares = input.nextInt();

在循环的第一次迭代中,您首先将回车作为下一个股票名称传递。您可以改为使用:

double sharePrice = Double.parseDouble(input.nextLine());

int numberOfshares = Integer.parseInt(input.nextLine());
于 2012-08-25T14:49:03.853 回答
0

您应该在阅读股价和股数后读出换行符:

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    double sharePrice = 0, stockPrice = 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();
        // Read out the newline character after the share price.
        input.nextLine();
        System.out.println("Enter the number of owned shares");
        int numberOfshares = input.nextInt();
        // Read out the newline character after the number of shares.
        input.nextLine();
        stockPrice = sharePrice * (double) (numberOfshares);
        sum += stockPrice;
        System.out.println(String.format("Name: %s, Price: %f, Shares: %d, Total: %f",
                                         name,
                                         sharePrice,
                                         numberOfshares,
                                         stockPrice));
        i++;
    }
    System.out.println("the total stockprice owned is: " + sum);
}

请参阅上面带有注释的行:

于 2012-08-25T14:51:42.387 回答