再次播放功能问题

时间:2017-10-02 23:28:48

标签: java

该程序以它应该的方式工作,但是用户输入他们是否想要再次播放然后在此之后什么也不做。我将所有原始输入值设置为0,这可能是原因。当用户想要再次播放时如何从用户那里获得新的输入。如果有人知道更好的方法使这个程序没有数组我会非常感谢你,如果你可以发布下面的代码。

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();
    }

}

}

2 个答案:

答案 0 :(得分:1)

您只提示用户输入一次,然后将它们设置为0.在循环的下一次迭代中,shape为0,因此if个语句都没有评估为true,因此您的程序什么都不做。

检索循环内的用户输入并删除变量清零:

public static void main(String[] args) {
    Scanner userInput = new Scanner(System.in);

    do {
        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");

        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();
        }
    } while (playAgain());
}

答案 1 :(得分:0)

您需要在do / while循环内提示用户输入。现在你正在提示用户正好在循环之上 - 所以它只在你的代码中发生一次。

//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())

值得注意的是,如果您只是在调试器中单步调试代码,就很容易发现。