2016-12-06 13 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")){ 
関連する問題