1 Attachment(s)
Hey guys, my While loop isn't working correctly.
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:
Code :
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:
Code :
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("");
}
}
}
Re: Hey guys, my While loop isn't working correctly.
Why don't you use a while(true) condition for your while loop, ask the user if they want to buy another ship, if they do then continue to loop and if they don't then return false. I'm not able to go through the actual code through the moment, so I can't say anything else about your other problem at the moment
Re: Hey guys, my While loop isn't working correctly.
Quote:
Originally Posted by
Actinistia
Why don't you use a while(true) condition for your while loop, ask the user if they want to buy another ship, if they do then continue to loop and if they don't then return false. I'm not able to go through the actual code through the moment, so I can't say anything else about your other problem at the moment
I'm not actually sure how I would do that. I need the user to input "Yes", "yes", "Y", or "y" to continue, so how would I make that into a true/false condition?
Or could I throw an If statement at the end of the loop asking if they want another, and if no, then break the loop? I don't know what the most efficient way of doing this would be.
Re: Hey guys, my While loop isn't working correctly.
Yes, I'm pretty sure the second option would work. I'm pretty new to java, to be honest, but I think that the if statement would do the job. You can pretty much make the if statement the same as your while loop condition (i.e. making the user input "No, no, N, n") and tell the program to break if they input it.
*side note*
Since it's a string, I was wondering if it'd be easier if you'd use the string.toLowercase() or string.toUppercase() to make it just 2 inputs. But since the way you have it works, I'd just keep it as it is.
Re: Hey guys, my While loop isn't working correctly.
Quote:
Originally Posted by
Actinistia
Yes, I'm pretty sure the second option would work. I'm pretty new to java, to be honest, but I think that the if statement would do the job. You can pretty much make the if statement the same as your while loop condition (i.e. making the user input "No, no, N, n") and tell the program to break if they input it.
*side note*
Since it's a string, I was wondering if it'd be easier if you'd use the string.toLowercase() or string.toUppercase() to make it just 2 inputs. But since the way you have it works, I'd just keep it as it is.
Using the string.toLowercase() thing is a good idea, I'll do that.
And my loop is still broken. Guess I'll completely rework the main class.
If anybody has anything else to contribute, I'll gladly take hints. :P
Re: Hey guys, my While loop isn't working correctly.
It is not good practice to use infinite while loops. Use a control variable!
Code Java:
String cont = "yes"; //Control variable
Scanner someScanner = new Scanner(System.in); //Scanner object
while(cont.toLowerCase().startsWith("y"))
{
/*
* All your code
*/
System.out.println("Should we run this loop again?"); //You should probably reword that, I just scanned your code
cont = someScanner.nextLine();
}
Re: Hey guys, my While loop isn't working correctly.
Quote:
Originally Posted by
Tjstretch
It is
not good practice to use infinite while loops. Use a control variable!
Code Java:
String cont = "yes"; //Control variable
Scanner someScanner = new Scanner(System.in); //Scanner object
while(cont.toLowerCase().startsWith("y"))
{
/*
* All your code
*/
System.out.println("Should we run this loop again?"); //You should probably reword that, I just scanned your code
cont = someScanner.nextLine();
}
Well, that completely broke my program. It executes "Would you like to buy a ship?" and "What is the name of the ship?" at the same time, not taking any input for "Would you like to buy a ship?". It used to only do that when the loop ran the 2nd time, and now it does it automatically. :[