2012-08-25 42 views
1

即时在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 

什么使得输入在第二个循环中跳过?

+0

它可能是由于你的需要来处理线令牌年底获得nextInt()或nextDouble()之后。考虑放入下一行() –

+1

请为代码块使用一致的逻辑缩进。这样做会使代码流更易于理解。这对于像条件语句或循环语句这样的事情尤其重要。 –

回答

3

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

解决的办法是让你的int和double为了吞下EOL令牌后使用input.nextLine()

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); 
+0

我试过一个不同的技巧,它的工作原理与你的一样: 所以我改变了 input.nextLine()input.next() 但是,你能解释一下当你的意思是吞下行尾?我假设input.nexLine将Line作为字符串输入?对?而.next()工作,因为字符串输入未填写? – user1624630

3

的问题是,上次你在第一次调用

int numberOfshares = input.nextInt(); 

循环迭代你的第一个传递回车作为下一个股票名称。你也可以使用:

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

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

没必要用'的Integer.parseInt(...)'因为扫描仪#nextInt()'将解析INT输入就好了,虽然这个解决方案*将*工作1+ –

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

见上面注释行:

+0

我尝试了不同的技巧和它的工作相同如你: 所以我改变了input.nextLine()来input.next() 但是,你能解释一下发生在你的燕子线的到底是什么意思?我假设input.nexLine将Line作为字符串输入?对?而.next()工作,因为字符串输入未填写? \t \t而(I <3) \t \t { \t \t的System.out.println( “输入股票名称”); \t \t name = input。下一个(); \t \t System.out.println(“输入股价”); sharePrice = input.nextDouble(); @关闭 – user1624630