2016-12-06 68 views
-1

import java.util.Scanner;將字符串轉換爲int並將其相乘

公共類predictStockMarket {

public static void main(String args[]){ 
    int openingPrice; 
    int currentPrice; 
    int numberOfStocks; 
    String buyOrSell; 

    Scanner number=new Scanner(System.in); 
    Scanner text=new Scanner(System.in); 

    System.out.println("What is the Opening Price? ");//Asking for the opening price 
    openingPrice=number.nextInt();   //storing into current price variable 

    System.out.println("What is the Current Price? ");//Asking for the Current Price 
    currentPrice=number.nextInt();   //storing into current price variable 

    System.out.println("Do you want to buy or sell? ");//Asking if they want to buy or sell 
    buyOrSell=text.nextLine();   

    if (buyOrSell.equalsIgnoreCase("buy")){ 
      System.out.println("How many stock do you want to buy ");//Asking the user for hours per week 
     buyNumber=number.nextLine(); 
     int i; 

     i = (buyNumber*currentPrice)-(openingPrice*buyNumber); 
     System.out.println(i); 

     } 
     else { 
      System.out.println("How many stock do you want to sell"); 
      sellNumber=number.nextLine(); 

      int i; 
      i = (sellNumber*currentPrice)-(openingPrice*sellNumber); 
      System.out.println(i); 

    } 

極其混亂,我認爲把一個局部變量將使它的整數。 使用Jgrasp

+2

嗯,是的,您沒有爲'buyOrSell'分配任何值。當你將它與「買」進行比較時,你期望它有什麼價值?你確實意識到你已*註釋掉了否則會分配一個值的行,對嗎? –

回答

1

取消註釋該行:

// buyOrSell=text.nextLine(); 

既然是評論說,沒有值分配給它。如果您嘗試訪問它,會導致問題:

if (buyOrSell.equalsIgnoreCase("buy")){