【问题标题】:Flaws in guessing game Exceptions and no correct answer猜谜游戏中的缺陷异常和没有正确答案
【发布时间】:2017-05-09 11:42:15
【问题描述】:
 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"))

然后当我认为我已经覆盖了它时它抛出异常的代码(当然我已经在前面的代码中输入了初始化)但这是最新的代码,因为我想尝试看看它是否仍然有效但是它没有

更新代码

  // 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;
}
}

如果你尝试测试它(我不会是关闭代码)它不会运行,因为它通过了一个异常

【问题讨论】:

  • 请调试您的应用程序。这是了解自己的最简单方法。另外:正确缩进你的代码,这会让事情变得更容易。
  • 有什么异常?
  • 为什么你的代码中有 7 个,有时是 8 个空行?至少您应该在此处发布之前删除这些内容。

标签: java random


【解决方案1】:

我想这就是你想要的。我删除了 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;
            }
        }

【讨论】:

  • 如果这是您要查找的内容,请将其标记为已回答。如果不是,我很乐意提供更多帮助
  • 不完全是关机代码关闭计算机。我想保留它。 getRandom 代码运行良好,虽然我会将它应用到我的 Geussing 游戏中,但我想让我的代码运行 就像我的更新版本一样,您可以在那里查看^^^^^^
  • 现在试试,我觉得应该可以了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多