简体   繁体   中英

My game works perfectly in Eclipse, but doesn't work in texteditor (ubuntu while using terminal). Why?

I am a first year CS student and I had to make a guessing game in java. It works fine in Eclipse, but in ubuntu terminal the program barely works. It only asks me once for a guess of a number and that's it.

After I give my answer it doesn't ask me again like it does in Eclipse.

Does anyone know how to fix? Sorry that it's in dutch.

public class Opgave0 {

public static void main(String[] args) {
    int x;
    Random random = new Random();
    int getal = 0;
    System.out.println("Geef een getal tussen 1 en 10, je mag drie keer raden.");
    System.out.println("Eerste keer:");
    Scanner Scanner = new Scanner(System.in);
    getal = Scanner.nextInt();
    x = random.nextInt(9) + 1;
    if (getal > 10) {
        System.out.println("Het getal is te hoog, kies een getal van 1 tot 10 en probeer het opnieuw.");
    }
    else if (getal < 1) {
        System.out.println("Het getal is te laag, kies een getal van 1 tot 10 en probeer het opnieuw.");
    }
    else {
        if (getal > 0 && getal < 10) {
            if (getal == x) {
                System.out.println(x + " klopt, goed geraden!");
            } 
            else if (getal > x) { 
                System.out.println("te hoog");
                System.out.println("Tweede keer:");
                getal = Scanner.nextInt();
                if (getal == x) {
                    System.out.println(x + " klopt, goed geraden!");
                }
                else if (getal > x) {
                    System.out.println("te hoog");
                    System.out.println("Derde keer:");
                    getal = Scanner.nextInt();
                    if (getal == x) {
                        System.out.println(x + " klopt, goed geraden!");
                        }
                    else if (getal > x) {
                        System.out.println("te hoog");
                        }
                    else if (getal < x) {
                        System.out.println("te laag");
                        }
                }
                else if (getal < x) {
                    System.out.println("te laag");
                    System.out.println("Derde keer:");
                    getal = Scanner.nextInt();
                    if (getal == x) {
                        System.out.println(x + " klopt, goed geraden!");
                        }
                    else if (getal > x) {
                        System.out.println("te hoog");
                        }
                    else if (getal < x) {
                        System.out.println("te laag");
                        }
                }

            }
            else if (getal < x) {
                System.out.println("te laag");
                System.out.println("Tweede keer:");
                getal = Scanner.nextInt();
                if (getal == x) {
                    System.out.println(x + " klopt, goed geraden!");
                }
                else if (getal > x) {
                    System.out.println("te hoog");
                    System.out.println("Derde keer:");
                    getal = Scanner.nextInt();
                    if (getal == x) {
                        System.out.println(x + " klopt, goed geraden!");
                        }
                    else if (getal > x) {
                        System.out.println("te hoog");
                        }
                    else if (getal < x) {
                        System.out.println("te laag");
                        }
                }
                else if (getal < x) {
                    System.out.println("te laag");
                    System.out.println("Derde keer:");
                    getal = Scanner.nextInt();
                    if (getal == x) {
                        System.out.println(x + " klopt, goed geraden!");
                        }
                    else if (getal > x) {
                        System.out.println("te hoog");
                        }
                    else if (getal < x) {
                        System.out.println("te laag");
                        }
                }
            }
            else {
                System.out.println("Kies een getal tussen 1 en 10, jouw getal is te hoog/laag");

            }

            }
        }
    }
}

您没有使用循环,因此您发布的代码只会执行一次(如您从命令行中看到的) 在 eclipse 中可能会多次询问的唯一方法是您实际上执行了多次。

There is no loop in your code. So keep your programme up put your all implementation which contains within main method in to a infinite loop.

while(true){
   //Your implementation for the game
}

It seems that your terminal console does not return the correct 'line-return character'. Try in your console-preferences to set the correct character encoding (UTF-8? try it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM