Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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" ?
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started