2012-08-25 63 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