Results 1 to 5 of 5
Thread: Ca program
- 09-30-2012, 03:45 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 22
- Rep Power
- 0
Ca program
I was seeing if someone can take a look at my code for my program as I have two issues...
if I hit 2 or 3 to start the program it returns an error but also still stores the variable as you can see here...
Start the app
1: Start the car
2: Drive the car
3: Stop the car
-1: Ends the program
Enter a selection by typing 1, 2, or 3: 2
You are driving the car!
1: Start the car
2: Drive the car
3: Stop the car
-1: Ends the program
Enter a selection by typing 1, 2, or 3:
My other issue is even after I hit 1 to start the program it starts over so the only thing I can type is 1 to start the car
Start the app
1: Start the car
2: Drive the car
3: Stop the car
-1: Ends the program
Enter a selection by typing 1, 2, or 3: 2
You are driving the car!
1: Start the car
2: Drive the car
3: Stop the car
-1: Ends the program
Enter a selection by typing 1, 2, or 3:
Here is my code I believe the problem is in the following code and will put the rest of my code after...
Here is the rest of my code...Java Code://if statments //if(nova == 1) //{ myCar.start(nova); //} if(nova == 2) { myCar.drive(nova); } if(nova == 3) { myCar.stop(nova); }
Java Code:/* Programmer: Bryan Kruep Date: 9/11/2012 Assignment: ProgrammingAssignment3 */ import java.util.Scanner; public class MyApp { public static void main(String [] args) { int nova; System.out.println("Start the app"); Car myCar = new Car(); do { Scanner keyboard = new Scanner(System.in); System.out.println("1: Start the car"); System.out.println("2: Drive the car"); System.out.println("3: Stop the car"); System.out.println("-1: Ends the program"); System.out.print("Enter a selection by typing 1, 2, or 3: "); nova = keyboard.nextInt(); while((nova != 1) && (nova != 2) && (nova != 3) && (nova != -1)) { System.out.println("This is not a valid selection!"); System.out.print("Make a selection again: "); nova = keyboard.nextInt(); } //if statments //if(nova == 1) //{ myCar.start(nova); //} if(nova == 2) { myCar.drive(nova); } if(nova == 3) { myCar.stop(nova); } }while(nova != -1); } } //class car class Car { //public int watts = 100; private boolean isStarted = true; public void start(int nova) { Scanner keyboard = new Scanner(System.in); if(nova != 1) isStarted = false; while((nova != 1) && (nova != -1)) { System.out.println("You must first start the car to drive or stop it!"); System.out.print("Make a selection again: "); nova = keyboard.nextInt(); } showState(nova); } public void drive(int nova) { /*Scanner keyboard = new Scanner(System.in); System.out.println("Please make your decision on what you want the car to do next!"); System.out.println("1: Start the car"); System.out.println("2: Drive the car"); S/tem.out.println("3: Stop the car"); System.out.print("What would you like the car to do next: "); nova = keyboard.nextInt();*/ Scanner keyboard = new Scanner(System.in); while ((nova == 1) && (nova != 2) && (nova != 3) && (nova != -1)) { System.out.println("This is not a valid selection!"); System.out.print("Make a selection again: "); nova = keyboard.nextInt(); } showState(nova); } public void stop(int nova) { /*Scanner keyboard = new Scanner(System.in); System.out.println("Please make your decision on what you want the car to do next!"); System.out.println("1: Start the car"); System.out.println("2: Drive the car"); System.out.println("3: Stop the car"); System.out.print("What would you like the car to do next: "); Scanner keyboard = new Scanner(System.in); nova = keyboard.nextInt();*/ Scanner keyboard = new Scanner(System.in); while(nova == 1) { System.out.println("This is not a valid selection!"); System.out.print("Make a selection again: "); nova = keyboard.nextInt(); } showState(nova); } public String showState(int nova) { switch(nova) { case 1: System.out.println("You have started the car!"); break; case 2: System.out.println("You are driving the car!"); break; case 3: System.out.println("You have stopped the car!"); break; } return "" + nova; } }
- 09-30-2012, 05:21 PM #2
- 09-30-2012, 11:56 PM #3
Member
- Join Date
- Jan 2008
- Posts
- 22
- Rep Power
- 0
Re: Ca program
I apologize as I did not interpret it correctly. It does not make an error but it just doesn't return an error message when it should.
- 10-01-2012, 02:46 AM #4
Re: Ca program
Clear as mud. Errors are thrown, not returned.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-02-2012, 05:34 AM #5
Member
- Join Date
- Jan 2008
- Posts
- 22
- Rep Power
- 0
Re: Ca program
I don't get error messages but here are my problems. When I start the program I am trying to set it up to where if I don't enter a 1 it will return an error and this part is correct. However, after I do hit the 1 it pretty much just loops my program and starts over again when I want it to test what the car would do next and it just starts over. The other thing it does When i hit like 2 to start the program and then hit 1 it stores both numbers which is not what I want it to do either. It is due tomorrow but if there is anything you can see please let me know.
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks