Question: I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n). First: I

I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n).

First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that?

Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ?

I have the following java-program, that creates a random walk starting at

(0,0) and continuing until x or y is greater than abs(n). First:

1 import java.util.*; 3 public class RandomWalkf 4 5 public static void main(String args)I 6 Scanner readernew Scanner(System. in); System.out.print("Enter the size of grid: "); // Taking input of the size of the grid 10 do [ try int n = reader.nextInt(); if(n 0) walk(n); System.out.printlnC"nEnter the size of grid: "); // Makes sure the program catches strings as an error. Prints error message and clears Scanner catch (InputMismatchException e) f 23 24 25 26 27 System.out.println("Input is not an integer. Please enter a positive (0 to terminate): "; reader.nextLineO } while (true); reader.slse) 28 29 30 31 public static void givePos(int X, int ) 32 //A function to print the position System.out, format(" Position-(Xd %d) ",X,Y); , 34

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!