如何让我的程序继续?

时间:2014-10-03 19:28:49

标签: java

我正在编写一个能吸引“cootie”怪物的程序。玩家输入一个数字,现在为1或2,程序为他们绘制怪物。 (A 1给你身体,2给你头部但是,在你能画出头部之前,你必须先画出身体)

我无法弄清楚如何让我的程序继续运行直到用户同时绘制头部和身体。现在,我的程序在结束之前只获得1个用户输入,即使玩家尚未绘制整个怪物。感谢帮助!

import java.util.Scanner;

public class Cootie {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("BODY PARTS:");
        System.out.println("1 = body");
        System.out.println("2 = head");
        System.out.println("3 = one leg");
        System.out.println("4 = one antenna");
        System.out.println("5 = one eye");
        System.out.println("6 = tail");
        System.out.println("What number did you roll?: ");

        int rollValue = scanner.nextInt();
        int body = 0;
        int error = 0;
        int head = 0;

        if (rollValue == 1) {
            if (body == 0) {
                body = 1;
            } else {
                error = 1;
            }
        } else if (rollValue == 2) {
            if (body == 1 && head == 0) {
                head = 1;
            } else {
                error = 1;
            }
        } else {
            System.out.println("Enter a valid input!");
        }

        if (body == 1) {
            System.out.println("You got the body!");
            System.out.println(" ");
            System.out.println(" ");
            System.out.println("   [ ]");
            System.out.println(" ");
            System.out.println("   [ ]");
            System.out.println(" ");
            System.out.println("   [ ]");
        } else if (head == 1) {
            System.out.println("You got the head!");
            System.out.println(" ");
            System.out.println("  (    )");
        }

        if (error == 1) {
            System.out.println("Can't add body part!");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这里有一些伪代码。您需要不断执行主要代码,直到您收到足够的代码来绘制您的Cootie怪物。扫描仪只需要输入一个输入,除非包裹在循环中

boolean done = false
while(!done){
    All of the code in your main
    if(condition that means cootie monster is finished)
       done = true;
}

答案 1 :(得分:0)

这是一个给你一些想法的例子

<强>代码

    List<String> list = new ArrayList<>();
    do {
        System.out.println("what is going on?");
        Scanner input = new Scanner(System.in);
        list.add(input.next());
     } while(!(list.contains("hi") && list.contains("bye")));

    System.out.println("you said hi and bye");

<强>输出

enter image description here

说明:在代码中,只要你没有输入hi和bye,循环就会循环

相关问题