SIDDHARTH VADER SIDDHARTH VADER - 11 months ago 100
Java Question

Play again feature issue

The program works the way it should but gets user's input on whether they want to play again or not and then does nothing after that. I am setting all of the original input values to 0 which is probably why. How can I get a new input from the user when they want to play again. If someone knows a better way make this program without arrays I would greatly appreciate it if you could post the code below.

import java.util.Scanner;
import java.util.*;

public class Shapes {

public static void main(String[] args) {

Scanner userInput = new Scanner(System.in);
System.out.print("You can view any of the following shapes: ");
System.out.print("\n1 Square");
System.out.print("\n2 Right-angle Triangle");
System.out.print("\n3 Pyramid");
System.out.print("\n4 Hourglass");
System.out.print("\n5 Diamond");

System.out.print("\nEnter a integer to choose a shape: ");
String shape = userInput.nextLine();

System.out.print("\nEnter the height of the shape: ");
int inputOne = userInput.nextInt();

System.out.print("Enter a character: ");
char ch = userInput.next().charAt(0);
System.out.println("\n");

do {

if (shape.equalsIgnoreCase("1")) {
square(ch, inputOne);
System.out.println();
}

if (shape.equalsIgnoreCase("2")) {
triangle(ch, inputOne);
System.out.println();
}

if (shape.equalsIgnoreCase("3")) {
pyramid(ch, inputOne);
System.out.println();
}

if (shape.equalsIgnoreCase("4")) {
diamond(ch, inputOne);
System.out.println();
}

if (shape.equalsIgnoreCase("5")) {
hourglass(ch, inputOne);
System.out.println();
}
shape = "0";
inputOne = 0;
ch = 0;

} while (playAgain());
}

private static boolean playAgain() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Play again? (Y/N): ");
String replay = keyboard.nextLine();
return replay.equalsIgnoreCase("Y");

}

public static void square(char c, int n) {

char[] a = new char[n];
Arrays.fill(a, c);
for (; n-- > 0;)
System.out.println(a);

}

public static void triangle(char c, int n) {

for (int i = 1; i <= n; i++) {
char[] a = new char[i];
Arrays.fill(a, c);
System.out.println(a);
}

}

public static void pyramid(char c, int n) {

for (int i = 0; i < n; i++) {
while (true) {
char[] s = new char[n - i - 1];
Arrays.fill(s, ' ');
System.out.print(s);

char[] a = new char[i * 2 + 1];
Arrays.fill(a, c);
System.out.println(a);
}
}

}

public static void diamond(char c, int n) {
boolean odd = n % 2 == 1;
n++;
int mid = n / 2;
int mi = mid;
if (odd)
mi--;
for (int y = 1; y < n; y++) {
for (int x = 1; x < n; x++) {
System.out.print((Math.abs(x + y - n) > mi || Math.abs(x - y) > mi) ? ' ' : c);
}
System.out.println();
}

}

public static void hourglass(char c, int n) {
boolean odd = n % 2 == 1;
if (odd)
n++;
int mid = n / 2;
for (int y = 0; y < n; y++) {
if (odd && y == mid)
continue;
for (int x = 1; x < n; x++) {
int a = 0;
if (Math.abs(x + y - mid) >= mid)
a++;
if (Math.abs(x - y - mid) >= mid)
a++;
System.out.print((a % 2 == 0) ? c : ' ');
}
System.out.println();
}

}

}

Answer Source

You need to prompt for the user input inside of the do/while loop. Right now you are prompting the user right above the loop - so it only happens once in your code.

//move this stuff into the do/while
Scanner userInput = new Scanner(System.in);
System.out.print("You can view any of the following shapes: ");
System.out.print("\n1 Square");
System.out.print("\n2 Right-angle Triangle");
System.out.print("\n3 Pyramid");
System.out.print("\n4 Hourglass");
System.out.print("\n5 Diamond");

System.out.print("\nEnter a integer to choose a shape: ");
String shape = userInput.nextLine();

System.out.print("\nEnter the height of the shape: ");
int inputOne = userInput.nextInt();

System.out.print("Enter a character: ");
char ch = userInput.next().charAt(0);
System.out.println("\n");

do {
    ^^^^ move that stuff in here
}while(playAgain())

It's also worth noting that this would be easy to discover if you simply step through your code in a debugger.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download