字符串到int然后乘以它

时间:2016-12-06 19:43:42

标签: java jgrasp

import java.util.Scanner;

公共课预测StockMarket {

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

1 个答案:

答案 0 :(得分:1)

取消注释这一行:

// buyOrSell=text.nextLine(); 

由于已注释,因此未为其分配任何值。如果您尝试访问它会导致问题:

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