简体   繁体   English

JAVA初学者帮助需要在循环中输入字符串

[英]JAVA beginner help needed string input in a loop

im a beginner in Java, and i have a problem to do: the problem prompts the user 5 times to enter, the Name of a stock then the Share Price then the number of shares owned and we should calculate the sum of all the stock values, i wrote it using only two prompts using a loop, but my issue is that, in the second prompt time the loop Skips the String input for the second Name of stock instead of promting...bellow is the code: 我是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 );
    }
}

And this is the output i get: 这是我得到的输出:

  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

What makes the input skip during the second loop? 是什么使输入在第二个循环中跳过?

Again as per my comment the problem is that your code doesn't handle the End Of Line (EOL) token when calling nextInt() or nextDouble() . 再次根据我的评论,问题是您的代码在调用nextInt()nextDouble()时不处理行尾(EOL)令牌。

The solution is to use input.nextLine() after getting your int and double in order to swallow the EOL token: 解决方案是在获取int值和double值后使用input.nextLine() ,以便吞下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);

The problem is that the last time you call 问题是您上次致电

int numberOfshares = input.nextInt();

in the first iteration of the loop your first passes a carriage return as the next stock name. 在循环的第一次迭代中,您的第一个传递回车符作为下一个股票名称。 You could instead use: 您可以改用:

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

and

int numberOfshares = Integer.parseInt(input.nextLine());

You should readout the newline characters after reading the share price and number of shares: 阅读股价和股数后,您应该读出换行符:

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);
}

See the lines with comments above: 参见上面带有注释的行:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM