|
-
October 25th, 2011, 08:14 PM
#1
New to Java Need help with Homework
I have my program that does what it needs to but I want it to be able to do one more thing and i can't seem to figure out how. This program ask what make,year,speed of a car, I also have where you can have the car accelerate or brake. The program works if you accelerate or brake once. I want to be able to accelerate or break 5 times then show the speed. Can somebody point me into the right direction? Thanks for the help!
Code:
import java.util.Scanner;
public class Car{
Scanner kb = new Scanner(System.in);
private int yearModel;
private String make;
private int speed;
private int acc;
private int count = 0;
public Car() {
this.yearModel = yearModel;
this.make = make;
this.speed = speed;
}
public void SetMake(){
this.make = kb.next();
}
public void SetYearModel(){
this.yearModel = kb.nextInt();
}
public void SetSpeed(){
this.speed = kb.nextInt();
}
public void Accel() {
this.acc = kb.nextInt();
while (count <= 5)
count++;
{
if (this.acc == 5) {
this.speed = speed + 5;
} else if (this.acc == -5) {
this.speed = speed - 5;
} else if (this.acc == 0) {
this.speed = speed;
} else {
System.out.println("Goodbye");
System.exit(0);
}
}
}
// public String getMake() {
// return make;
//
// }
// public void setMake(String make) {
// this.make = make;
// }
// public int getSpeed() {
// return speed;
// }
// public void setSpeed(int speed) {
// this.speed = speed;
// }
// public int getYearModel() {
// return yearModel;
// }
// public void setYearModel(int yearModel) {
// this.yearModel = yearModel;
// }
@Override
public String toString() {
return "Car {" + "Year Model: " + yearModel
+ ", Make: " + make
+ ", Speed=" + speed + '}';
}
}
Code:
public class CarDemo {
public static void main(String[] args) {
Car car1 = new Car();
System.out.println("Enter Make:\t");
car1.SetMake();
System.out.println("Enter Year:\t");
car1.SetYearModel();
System.out.println("Enter Speed:\t");
car1.SetSpeed();
System.out.println("Enter 5 to accelerate Vehicle or Enter -5 to brake by 5 mph");
System.out.println("Enter Acceleration:\t");
car1.Accel();
System.out.println(car1);
-
October 26th, 2011, 12:31 AM
#2
Re: New to Java Need help with Homework
the problem is that you are executing a loop with no body
your loop is just this part
Code:
while (count <= 5)
count++;
none of the if statements get executed
you should put your increment at the end of the loop's body
Code:
while (count <= 5)
{
//if statements{
//}
count++;
} //end while loop
and by the way you have count = 0 and the while loop is set to test until <=5, so it will be executed 6 times not 5.
-
October 26th, 2011, 02:46 PM
#3
Re: New to Java Need help with Homework
Thanks small things make a difference.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|