More Solutions (doc)

advertisement
// ICE 8: PART 6 - Fix Keyboard SOLUTION
// ##################################################
import becker.robots.*;
import java.util.*; // <--- NOTICE THIS HERE
public class Fix_Keyboard_IO_SOLUTION extends Object
{
public static void main(String[] args)
{
City seattle = new City();
Robot mary = new Robot(seattle, 1, 1, Direction.EAST, 0);
System.out.println("Don't forget to push the 'Start' button, then click the 'Run I/O' window before typing!");
int numMoves = 0; // Notice that we can name the 'userChoice' variable anything
// Since we're asking how far to move the robot numMoves is a good choice
Scanner keyboard = new Scanner(System.in);
System.out.println ("How many intersections forward would you like the robot to go?");
if( keyboard.hasNextInt() )
{
numMoves = keyboard.nextInt();
// nextInt actually gets the input
System.out.println ("You asked to move " + numMoves + " spaces");
int counter = 0;
while( counter < numMoves)
{
mary.move();
counter = counter + 1;
}
}
else
{
System.out.println ("You did NOT type in a whole number!");
}
keyboard.nextLine(); // DON'T FORGET TO CLEAR ANY REMAINING INPUT!!
System.out.println ("THE PROGRAM ENDS HERE!!");
}
}
// ICE 8: PART 2 - INPUT / OUTPUT SOLUTION
//##################################################
import becker.robots.*;
import java.util.*; // <-- NOTICE
public class ICE_05_IO_SOLUTION extends Object
{
public static void main(String[] args)
{
int userChoice = 0 ;
Scanner keyboard = new Scanner(System.in);
City wallville = new City(10, 10);
Robot rob = new Robot(wallville, 7, 0, Direction.EAST, 0);
new Thing(wallville, 7, 0);
new Thing(wallville, 7, 6);
new Thing(wallville, 1, 6);
while(userChoice != 3)
{
System.out.println("Please select from the following options:");
System.out.println("1 - turn right");
System.out.println("2 - move");
System.out.println("3 - quit");
if(keyboard.hasNextInt()) // if(!keyboard.hasNextInt()) WOULD WORK HERE TOO BUT REQUIRE DIFFERENT LOGIC BELOW
{
userChoice = keyboard.nextInt();
System.out.println("");
System.out.println("User input is: " + userChoice);
System.out.println("");
if( userChoice == 1)
{
rob.turnLeft();
rob.turnLeft();
rob.turnLeft();
}
if( userChoice == 2)
{
rob.move();
}
if( userChoice == 3)
{
System.out.println("Quitting. Goodbye.");
}
if( userChoice < 1)
{
System.out.println("");
System.out.println(userChoice + " is not on the menu");
System.out.println("");
System.out.println("Please try again.");
System.out.println("");
}
if( userChoice > 3)
{
System.out.println("");
System.out.println(userChoice + " is not on the menu");
System.out.println("");
System.out.println("Please try again.");
System.out.println("");
}
}
else
{
System.out.println("You have not input a valid integer");
keyboard.nextLine(); //<-- NEED THIS TO FLUSH THE CHARACTER FROM THE VARIABLE SPACE
}
}
}
}
// ICE 8: PART 2 (ALTERNATIVE) - INPUT / OUTPUT SOLUTION
//##########################################################
import becker.robots.*;
import java.util.*;
public class ICE_05_IO_SOLUTION_2 extends Object
{
public static void main(String[] args)
{
int userChoice = 0 ;
Scanner keyboard = new Scanner(System.in);
City wallville = new City(10, 10);
Robot rob = new Robot(wallville, 7, 0, Direction.EAST, 0);
new Thing(wallville, 7, 0);
new Thing(wallville, 7, 6);
new Thing(wallville, 1, 6);
while(userChoice != 3)
{
System.out.println("Please select from the following options:");
System.out.println("1 - turn right");
System.out.println("2 - move");
System.out.println("3 - quit");
if( keyboard.hasNextInt())
{
userChoice = keyboard.nextInt();
System.out.println("userInput is: " + userChoice);
if( userChoice == 1)
{
rob.turnLeft();
rob.turnLeft();
rob.turnLeft();
}
if( userChoice == 2)
{
rob.move();
}
if( userChoice == 3)
{
System.out.println("Goodbye for now.");
}
if( userChoice < 1)
{
System.out.println("");
System.out.println(userChoice + " is not on the menu");
System.out.println("");
System.out.println("Please try again.");
System.out.println("");
}
if( userChoice > 3)
{
System.out.println("");
System.out.println(userChoice + " is not on the menu");
System.out.println("");
System.out.println("Please try again.");
System.out.println("");
}
}
else
{
System.out.println("");
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("You have not input a valid integer. Release the Kraken!");
System.out.println("");
//keyboard.nextLine(); // <-- Move this OUTSIDE the else statement so it would work with the while loop
}
keyboard.nextLine();
}
}
}
Download