| A New Income |
A NEW INCOMEA New Income Q&A
Q: What is wrong with this Java code? I keep on getting the error 'else' without an 'if'. Does anyone know what is wrong. Here is the code: import java.util.Scanner; public class MyBankAccount { public static void main(String[]args) { //Declare variables double deposit; double balance; double yearlyIncome; int years; int numberOfDeposits; boolean inTheBlack; double lifeTimeGoal; double lifeTimeGoalBalance; String name; String pinNumber; Scanner input = new Scanner(System.in); //Asks for name, responds to some input, but not all. System.out.println("What is your name?"); name = input.nextLine(); if (name.equals("Kyle")) { System.out.println("You are my creator, and I thank you for that."); } else if (name.equals("kyle")) { System.out.println("You are my creator, and I thank you for that."); } else if (name.equals("Larry")) {System.out.println("Don't try to break my code. Also, you just lost the game."); } else if (name.equals("larry")) {System.out.println("Don't try to break my code. Also, you just lost the game.");} else { System.out.println("Hello, " + name); } //Asks for pin number, then leads you through depositing steps appropriate to the pin number. Feel free to add your own, or add new ones. System.out.println("What is your pin number?"); pinNumber = input.nextLine(); if (pinNumber.equals("42")){ System.out.println("Congratulations! You have read the Hitchhiker's Guide to the Galaxy! Since 42 is the answer to life, the universe, and everything that still applies at this bank"); System.out.println("This pin number is valid. You may continue."); balance = 8970; System.out.println("Your balance is $" + balance); System.out.println("How much would you like to deposit?"); deposit = input.nextDouble(); System.out.println("Your new balance is $" + (deposit+balance)); System.out.println("What is your yearly income?"); yearlyIncome = input.nextDouble(); System.out.println("What is your lifetime goal for the amount of money you have?"); lifeTimeGoalBalance = input.nextDouble(); System.out.println("You are $" + (lifeTimeGoalBalance - (deposit+balance)) + "away from your goal."); System.out.println("It will take you " + ((lifeTimeGoalBalance - (deposit+balance)) / yearlyIncome) + " years to reach your goal."); System.out.println("Have a nice day!");} else if (pinNumber.equals("6666")) { System.out.println("This pin number is valid. You may continue."); balance = 8970; System.out.println("Your balance is $" + balance); System.out.println("How much would you like to deposit?"); deposit = input.nextDouble(); System.out.println("Your new balance is $" + (deposit+balance)); System.out.println("What is your yearly income?"); yearlyIncome = input.nextDouble(); System.out.println("What is your lifetime goal for the amount of money you have?"); lifeTimeGoalBalance = input.nextDouble(); System.out.println("You are $" + (lifeTimeGoalBalance - (deposit+balance)) + "away from your goal."); System.out.println("It will take you " + ((lifeTimeGoalBalance - (deposit+balance)) / yearlyIncome) + " years to reach your goal."); System.out.println("Have a nice day!");} else if (pinNumber.equals("6")) { System.out.println("This pin number is valid. You may continue."); balance = 208937420; System.out.println("Your balance is $" + balance); System.out.println("How much would you like to deposit?"); deposit = input.nextDouble(); System.out.println("Your new balance is $" + (deposit+balance)); System.out.println("What is your yearly income?"); yearlyIncome = input.nextDouble(); System.out.println("What is your lifetime goal for the amount of money you have?"); lifeTimeGoalBalance = input.nextDouble(); System.out.println("You are $" + (lifeTimeGoalBalance - (deposit+balance)) + "away from your goal."); System.out.println("It will take you " + ((lifeTimeGoalBalance - (deposit+balance)) / yearlyIncome) + " years to reach your goal."); System.out.println("Have a nice day!");} else if (pinNumber.equals("666")) { System.out.println(" A: Your error is here: System.out.println("Have a nice day!");} else if (pinNumber.equals("6666")) { System.out.println("This pin number is valid. You may continue."); That "else" in the middle occurs before a matching "if". HTH
|
© 2010 anewincome.com Contact us - Q&A powered by Yahoo! Answers
| A New Income |