vendredi 24 février 2017

If statement not checking string condition [duplicate]

This question already has an answer here:

I'm writing a very basic stock market game. Here's the code I'm having trouble with.

if(yn == "y"){
    System.out.println("\nYour starting money is $" + df.format(usernum) +  "\nType \"quit\" to quit anytime\nEnter how many days you want to simulate: ");
    sim = input.nextInt();

I'm not done so far, but I was testing it out, and it seems that at the very first if statement, it doesn't check the scanner for entered strings. it skips another if else statement I have, and then it just goes to the else statement

else System.out.println("I said enter y or n!!!");

I tried

yn = input.nextLine();

just to make sure it wasn't a mistake. Is there something I'm missing? Can Strings even be checked in if statements? I'm guessing if it doesn't work now, then the ones in the first while loop will probably not work either. Any help is appreciated, and hopefully it's not a stupid mistake. I'm a beginner in Java. Below is the full code if needed.

import java.util.Scanner;
import java.util.Random;
import java.text.DecimalFormat;
public class StocksSim {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    DecimalFormat df = new DecimalFormat("#.##");
    Random rand = new Random();

    int sim;
    double apple = (500)*rand.nextDouble() + 1;
    double cocacola = (500)*rand.nextDouble() + 1;
    double google = (500)*rand.nextDouble() + 1;
    double yahoo = (500)*rand.nextDouble() + 1;
    double tmobile = (500)*rand.nextDouble() + 1;
    double nike = (500)*rand.nextDouble() + 1;
    double usernum = 1000;
    int numofdays = 0;
    int waiting = 0;
    int quantity;
    String stockname;
    String quit = "quit";
    String yn;
    boolean start = true;
    boolean over = false;

    System.out.println("Welcome to the Stocks game.\nRemember you can only simulate 10 days maximum.\nCareful simulating too many days. You might miss the stock of your life!\nContinue?(y/n)");
    yn = input.next();

    if(yn == "y"){
        System.out.println("\nYour starting money is $" + df.format(usernum) +  "\nType \"quit\" to quit anytime\nEnter how many days you want to simulate: ");
        sim = input.nextInt();

    while(start && !over && sim > 0 && sim<=10){

            numofdays = (sim + numofdays);
            System.out.print("Simulating " + sim + " day(s)");
            while(waiting <= 2){
            try{
                Thread.sleep(800);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.print(".");
            waiting++;
            }
            System.out.println("\nToday's stock prices are \n\nApple at  $" + df.format(apple) + " per share");
            try{
                Thread.sleep(800);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("\n\nCocaCola at  $" + df.format(cocacola) + " per share");
            try{
                Thread.sleep(800);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("\n\nGoogle at  $" + df.format(google) + " per share");
            try{
                Thread.sleep(500);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("\n\nYahoo at  $" + df.format(yahoo) + " per share");
            try{
                Thread.sleep(500);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("\n\nNike at  $" + df.format(nike) + " per share");
            try{
                Thread.sleep(500);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("\n\nNike at  $" + df.format(tmobile) + " per share");
            try{
                Thread.sleep(500);
            } catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("Enter the name of the stock you want to buy. (Enter the name exactly as written)\n");
            stockname = input.nextLine();
            if(stockname == "Apple"){
                System.out.println("How many shares do you want to buy?");
                quantity = input.nextInt();
                usernum = (usernum-(apple*quantity));
                if(usernum >= 0){
                    System.out.println("You bought " + quantity + " Apple share(s). \nYour remaining balance is now " + df.format(usernum));
                }
                else if(usernum < 0){
                    System.out.println("You can't make that transaction. Not enough money");
                }
            }
            else if(stockname == "CocaCola"){
                System.out.println("How many shares do you want to buy?");
                quantity = input.nextInt();
                usernum = (usernum-(cocacola*quantity));
                if(usernum >= 0){
                    System.out.println("You bought " + quantity + " CocaCola share(s). \nYour remaining balance is now " + df.format(usernum));
                }
                else if(usernum < 0){
                    System.out.println("You can't make that transaction. Not enough money");
                }
            }
            else if(stockname == "Google"){
                System.out.println("How many shares do you want to buy?");
                quantity = input.nextInt();
                usernum = (usernum-(google*quantity));
                if(usernum >= 0){
                    System.out.println("You bought " + quantity + " Google share(s). \nYour remaining balance is now " + df.format(usernum));
                }
                else if(usernum < 0){
                    System.out.println("You can't make that transaction. Not enough money");
                }
            }
            else if(stockname == "Yahoo"){
                System.out.println("How many shares do you want to buy?");
                quantity = input.nextInt();
                usernum = (usernum-(yahoo*quantity));
                if(usernum >= 0){
                    System.out.println("You bought " + quantity + " Yahoo share(s). \nYour remaining balance is now " + df.format(usernum));
                }
                else if(usernum < 0){
                    System.out.println("You can't make that transaction. Not enough money");
                }
            }
            else if(stockname == "Nike"){
                System.out.println("How many shares do you want to buy?");
                quantity = input.nextInt();
                usernum = (usernum-(nike*quantity));
                if(usernum >= 0){
                    System.out.println("You bought " + quantity + " Nike share(s). \nYour remaining balance is now " + df.format(usernum));
                }
                else if(usernum < 0){
                    System.out.println("You can't make that transaction. Not enough money");
                }
            }
            else if(stockname == "T-Mobile"){
                System.out.println("How many shares do you want to buy?");
                quantity = input.nextInt();
                usernum = (usernum-(tmobile*quantity));
                if(usernum >= 0){
                    System.out.println("You bought " + quantity + " T-Mobile share(s). \nYour remaining balance is now " + df.format(usernum));
                }
                else if(usernum < 0){
                    System.out.println("You can't make that transaction. Not enough money");
                }
            }
            else System.out.println("Please enter the correct name!");

        }

    }
        else if(yn == "n"){
            System.out.println("Game over. Come back next time!");
        }
        else System.out.println("I said enter y or n!!!");










}

}

Aucun commentaire:

Enregistrer un commentaire