Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Continuation of loops

Hey everyone please am having issues with my code not doing what I want it to do. When compiled and i run it, it only runs case 1 in the main method and goes back to the printing at the beginning of the main method.

Even when I enter “1” to turn the ignition on or off, ReportState always reports the ignition as being off. Although the default state is off i want it to change to on after the case 1 switch break.

When I enter “2” to specify a car movement, the program prints:In which direction do you want to move the car? H: Horizontal V: Vertical Direction: Enter a movement distance:

without giving me a chance to enter the direction before it asks for the distance. As I suspect, I don’t think the scan.nextLine() reads in a string. well. at each click to run it, it keeps going back to the beginning where it prints what would you like to do.

Please can someone help out with this.

public static void reportState(boolean ignition, int yPosition, int xPosition, char color ) {

                System.out.println("\n"+" Car information \n  Color : " + color + " \n ignition : " + ignition);

                for(int j=0; j<=19; j++){
                    for(int f=0; f<=19; f++){
                    if (f== yPosition && j == xPosition){
                    System.out.print("r");                      
                }

                     else {

           System.out.print("-");
                        }
                }
           System.out.println("-");
        }

        System.out.print ("-");
            //if (ignition = true) {
            //  iginitionSwitch(ignition); 
                //}

        // break;
    }


     public static void main (String[]args) {

         Random  rand = new Random();
        int valuey;
                   valuey = 1 + ( int ) ( Math.random() * 19 );
        int valuex;
                   valuex = 1 + ( int ) ( Math.random() * 19 );

         boolean ignition = false;
    //  char valuew;
                //  valuew =( char ) ( Math.random() * 5);
                 char color = 'r';

                 // if (color == valuew){
                  //  return R;
                  //   } 



         int xPosition = valuex;
         int yPosition = valuey;
         int x = 0;
         int y = 0;



    boolean finished = false;
while (!finished) 
{
 System.out.println("What would you like to do?  \n  1: turn the ignition on/off. \n 2: change the position of car.\n  Q: quit this program:" );
 Scanner scan = new Scanner(System.in);


  String op =scan.nextLine();



              switch (op) {

              case "1" :ignition = iginitionSwitch(ignition);
                        reportState(ignition, yPosition, xPosition, color );
                        String on = scan.nextLine();
                       System.out.println("\n"+" Car information \n  Color : " + color + " \n ignition : on " );
                    break;

              case "2": System.out.print("In which direction do you want to move the car? \n H: Horizontal \n V: Vertical Direction: ");
                    String direction1 =scan.nextLine();
                    System.out.print("Enter a movement distance: ");

                       Integer dist = scan.nextInt();
                       System.out.print("In which direction do you want to move the car ");
                       scan.nextLine();
                       if ( direction1 == "H" ) {
                xPosition = moveHorizontally(dist, xPosition, ignition);
                reportState(ignition, yPosition, xPosition, color );
                             break;                 
                                  }
                  else if ( direction1 == "V") {
                yPosition = moveHorizontally(dist, yPosition, ignition);
                reportState(ignition, yPosition, xPosition, color );

                           }

                break;

              case "Q": 
                finished = true;
                System.exit( 0 );

                } 
            }
          }

} 
Tagged:
Sign In or Register to comment.