2012-02-15 34 views
1

我一直在爲此奮鬥了一段時間,並找到了一對有用的資源,但問題仍然存在。如何獲得和利用Java中的下一個字符串

這裏是我的代碼:

import java.util.Scanner; 

public class Stocks { 

public static void main(String [] args){ 

    // value of stock at beginning of year and end of year. 
    final int beginningStock = 10; 
    final int endStock = 20; 


    // value of stocks by quarter; there are three quarters. 
    int firstQuarter; 
    int secondQuarter; 
    int thirdQuarter; 
    String broker; 
    String Buy; 

    firstQuarter = 10; 
    secondQuarter = 30; 
    thirdQuarter = 20; 

    //Tell client the maximum value/price of the stock during the year.  
    System.out.println("The maximum price of a stock share in the year is: $" + secondQuarter + "."); 

    // Tell client the minimum value/price of the stock during the year. 
    System.out.println("The minimum price of a stock share in the year is: $" + firstQuarter + "."); 

    //Ask broker if you want to buy or sell 
    Scanner input = new Scanner(System.in); 

    System.out.println("Would you like to Buy or Sell stocks? Please use Buy or Sell commands."); 
    broker = input.next(""); 

    if (broker == "Buy"){ 
     //Calculate percentage increase of stock through year if the broker wants the client to buy. 
     //The values are relative to the start of the year. 
     double percentIncrease; 

     percentIncrease = (double)(endStock - beginningStock)/(beginningStock); 

     //Convert decimal to percentage and tell client percentage increase relative to beginning of year. 
     System.out.println("The percentage increase of the stock through the year, relative to beginning of year, is: %"+ ((int)(percentIncrease*100+.5))+ "."); 


    } 

    else if (broker == "Sell"){ 
     //Calculate change relative to end of year 
     double endIncrease; 

     endIncrease = (double)(endStock - beginningStock)/(endStock); 

     //Convert decimal to percentage and tell client percentage increase relative to end of year. 
     System.out.println("The percentage increase of the stock through the year, relative to end of year, is: %"+ ((int)(endIncrease*100))+ "."); 

    } 

    } 
} 

我遇到的問題是圍繞線29:

//Ask broker if you want to buy or sell 
    Scanner input = new Scanner(System.in); 

    System.out.println("Would you like to Buy or Sell stocks? Please use Buy or Sell commands."); 
    broker = input.next(""); 

    if (broker == "Buy"){ 
     //Calculate percentage increase of stock through year if the broker wants the client to buy. 
     //The values are relative to the start of the year. 
     double percentIncrease; 

     percentIncrease = (double)(endStock - beginningStock)/(beginningStock); 

     //Convert decimal to percentage and tell client percentage increase relative to beginning of year. 
     System.out.println("The percentage increase of the stock through the year, relative to beginning of year, is: %"+ ((int)(percentIncrease*100+.5))+ "."); 

這將需要字符串,但不會使用它。我不知道我做錯了什麼。原諒我這是我在這裏的第一篇文章。

回答

2

問題是==測試引用相等而不是值相等;即檢查雙方是否爲相同的對象,而不是等效的對象。你需要改變這一點:

if (broker == "Buy"){ 

這樣:

if (broker.equals("Buy")){ 
0

"Buy".equals(broker)應該是條件檢查

在Java中的字符串比較應該是.equals不==

1

正如有人說,你應該使用equals()比較的String實例。

您在代碼中有另一個問題。您正在使用Scanner的方法next(String pattern),如果它與傳遞的模式匹配,將返回String。由於您傳遞了一個空的String作爲模式,它會引發異常。您應該使用next()

+0

謝謝大家!這非常有幫助!我希望我能夠投票給你們! – 2012-02-15 01:36:14

相關問題