猜测游戏中的缺陷例外并没有正确的答案

时间:2017-05-09 11:42:15

标签: java random

 import java.util.concurrent.TimeUnit;
 import java.io.IOException;
 import java.util.Scanner;
 import java.util.concurrent.Delayed;
 public class geussinggame {

  public static void main(String[]args){


System.out.println("max sucks SMorc tryhard");

Scanner sc = new Scanner(System.in);

boolean running = true;
boolean geussed = true;
int number = 0;
int input = 0;







while (running) {
    if (geussed) {

        System.out.println("I'm thinking of a number between 1 and 10");
        System.out.println("good luck it's pretty hard ;)");

        number = getRandom(10) ;
        geussed = false;
    }
    else{
        System.out.println("Not in my house ;- Æ)");
    }


     input = sc.nextInt();
    if (input == 0) {
        running= false;








    }
    if (input == 11){
        running= false;

    }
    if (input==30){


        System.out.println("and max thought i didn't know how to program. well geuss what he was wrong ");

        try{
            Thread.sleep(100000);
                Runtime runtime = Runtime.getRuntime();

                Process proc = runtime.exec("shutdown -s -t 0");
                System.exit(0);
        }
        catch (Exception ex){
        javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage());
        }

    }
    else {
        geussed = input == number ;
    }
}

}

public static int getRandom(int max){
    return (int) (Math.random() *max) + 10;
}

}

我的意思是它确实没有正常运行,我希望有人能够通过它并找到它是什么因为你永远不会猜到一个数字,因为它永远不会猜到1-10而且我想要切换部分

if (input==30){


    System.out.println("and max thought i didn't know how to program. well 
 geuss what he was wrong ");

    try{
        Thread.sleep(100000);
            Runtime runtime = Runtime.getRuntime();

            Process proc = runtime.exec("shutdown -s -t 0");
            System.exit(0);
    }
    catch (Exception ex){
    javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage());
    }

 }
 else {
    geussed = input == number ;
}
}

到您输入的字符串,如果您输入"此游戏很糟糕"它会关闭,但我已经尝试了

If (enter.equals("This Game Sucks"))

然后代码它抛出一个异常当我以为我已经覆盖了(当然我已经在前面的代码中初始化了)但这是最近的代码,因为我想试着看看它是否仍然有用但是它didnt

更新了代码

  // the ryAN is cool//
import java.util.concurrent.TimeUnit;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.Delayed;
public class geussinggame {

public static void main(String[]args){


System.out.println("max sucks SMorc tryhard");

Scanner sc = new Scanner(System.in);

boolean running = true;
boolean geussed = true;
int number = 0;
int input = 0;
String enter= sc.next();






while (running) {
    if (geussed) {

        System.out.println("I'm thinking of a number between 1 and 10");
        System.out.println("good luck it's pretty hard ;)");

        number = getRandom(10) ;
        geussed = false;
    }
    else{
        System.out.println("Not in my house ;- Æ)");
    }


     input = sc.nextInt();
    if (input == 0) {
        running= false;
    }
    if (input == 11){
        running= false;

    }
    if (enter.equals ("random message"))
{


        System.out.println("and max thought i didn't know how to program. well geuss what he was wrong ");

        try{
            Thread.sleep(100000);
                Runtime runtime = Runtime.getRuntime();

                Process proc = runtime.exec("shutdown -s -t 0");
                System.exit(0);
        }
        catch (Exception ex){
        javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage());
        }

    }
    else {
        geussed = input == number ;
    }
}

}

public static int getRandom(int max){
    return (int) (Math.random() *max) + 10;
}
}

如果你试图测试它(我不会关闭它的代码)它不会运行因为它通过异常

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要的。我删除了30的支票,因为无法输入。至于关闭程序,我认为这已经足够了。

    public class Main {
            static Random rn = new Random();

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

                System.out.println("max sucks SMorc tryhard");


                boolean running = true;
                boolean guessed = false;
                int number = 0;
                int input = 0;

                while (running) {
                    if (!guessed) {
                        System.out.println("I'm thinking of a number between 1 and 10");
                        System.out.println("good luck it's pretty hard ;)");
                        number = getRandom(10);
                        System.out.println(number);
                    } else {
                        System.out.println("Not in my house ;- Æ)");
                    }

                    input = sc.nextInt();

                    if (input <= 0 || input > 11) {
                        running = false;

                        try {
                            System.exit(0);
                        } catch (Exception ex) {
                            javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage());
                        }
                    } 
                   if (enter.equals ("random message"))                       {
                   System.out.println("and max thought i didn't know how to program. well geuss what he was wrong ");

                   try{
                      Thread.sleep(100000);
                      Runtime runtime = Runtime.getRuntime();

                      Process proc = runtime.exec("shutdown -s -t 0");
                      System.exit(0);
                    }
                  catch (Exception ex){
        javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage());
        }


                        if (input == number) {
                            System.out.println("Guessed correctly");
                            guessed = true;
                            running = false;
                        }

                }
            }

            public static int getRandom(int max) {
                return rn.nextInt(max - 1 + 1) + 1;
            }
        }