1

私は 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 番目のループで入力がスキップされる原因は何ですか?

4

3 に答える 3

3

nextInt()繰り返しますが、私のコメントによると、問題は、またはを呼び出すときにコードがEnd Of Line(EOL)トークンを処理しないことnextDouble()です。

input.nextLine()解決策は、EOLトークンを飲み込むために、intとdoubleを取得した後に使用することです。

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 に答える