Okay, I attached the program specifications. Basically, I need to make a "salesman" program selling rocket ships. My problem is that the while loop isn't working correctly.
It runs, but when it runs a 2nd time, it executes the line that asks what the name of the ship is and the line asking for how many shields. I also don't know how to make the loop end when the user responds with "no".
Basically, the loop just doesn't work correctly, and I need help. I've run into a brick wall with this assignment.
Here's the code for the main class:
package lab05; import java.util.Scanner; import java.util.Random; import java.text.DecimalFormat; public class Lab05 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); Random PRNG = new Random(); DecimalFormat formatter = new DecimalFormat("#0.00"); Spaceship ship1 = new Spaceship(); System.out.print("Would you like to buy a ship? "); String ans = keyboard.nextLine(); while(ans.equals("Yes")||ans.equals("yes")||ans.equals("Y")||ans.equals("y")) { System.out.print("What is the name of the ship? "); String tempName = keyboard.nextLine(); ship1.setName(tempName); System.out.print("How many shields? "); int tempShields = keyboard.nextInt(); ship1.setNumShields(tempShields); System.out.print("How many guns? "); int tempGuns = keyboard.nextInt(); ship1.setNumGuns(tempGuns); System.out.print("How many engines? "); int tempEngines = keyboard.nextInt(); ship1.setNumEngines(tempEngines); int rand = PRNG.nextInt(10)+1; switch (rand) { case 1: ship1.setNumDroids(1); break; case 2: ship1.setNumDroids(1); break; case 3: ship1.setNumDroids(1); break; case 4: ship1.setNumDroids(2); break; case 5: ship1.setNumDroids(2); break; case 6: ship1.setNumDroids(0); break; case 7: ship1.setNumDroids(0); break; case 8: ship1.setNumDroids(0); break; case 9: ship1.setNumDroids(0); break; case 10: ship1.setNumDroids(0); break; default: ship1.setNumDroids(0); break; } ship1.displayName(); ship1.displayNumShields(); ship1.displayNumGuns(); ship1.displayNumEngines(); ship1.displayNumDroids(); } } }
And here's the Spaceship class:
package lab05; public class Spaceship { public String nameShip; private int numShields; private int numGuns; private int numEngines; private int numDroids; public void setName(String name) { this.nameShip = name; } public String getName() { return nameShip; } public void displayName() { System.out.println("The name of the ship is " + getName()); } public void setNumShields(int numShields) { this.numShields = numShields; } public int getNumShields() { return numShields; } public void displayNumShields() { System.out.println("The ship has " + getNumShields() + " shield(s)"); } public void setNumGuns(int numGuns) { this.numGuns = numGuns; } public int getNumGuns() { return numGuns; } public void displayNumGuns() { System.out.println("The ship has " + getNumGuns() + " gun(s)"); } public void setNumEngines(int numEngines) { this.numEngines = numEngines; } public int getNumEngines() { return numEngines; } public void displayNumEngines() { System.out.println("The ship has " + getNumEngines() + " engine(s)"); } public void setNumDroids(int numDroids) { this.numDroids = numDroids; } public int getNumDroids() { return numDroids; } public void displayNumDroids() { if(numDroids > 0) { System.out.println("We're giving you " + getNumDroids() + " free droid(s)"); } else { System.out.print(""); } } }