简体   繁体   English

猜游戏的Java代码未打印任何内容

[英]Java code for guessing game not printing anything

So I have a Computer Science course at school in which we learn Java. 因此,我在学校开设了一门计算机科学课程,我们在其中学习Java。 We were assigned to do a simple text based recreation of the guessing game. 我们被分配去做一个简单的基于文本的猜谜游戏。 I got it done until this point, and I cannot seem to find where I messed up because there is nothing printed when I run the core. 到现在为止,我已经完成了它,但是由于运行内核时没有打印任何内容,因此我似乎找不到混乱的地方。

This is the code: 这是代码:

    public class GuessGame 
{
  public static void main(String[] args) 
  { 
    new GuessGame();
  }  
  public GuessGame () 
  { 
    char end = 'y';
    while (end!='y')
    {
      System.out.println ("Welcome to the Guessing Game!");
      System.out.println ("\nThe computer has picked a number");
      System.out.println ("between 1 and 100. Try to guess it.");
      int num = (int)(Math.random()*(100-1)+1);
      int guess = IBIO.inputInt ("Guess the number: ");
      if (guess==num)
        System.out.println ("You got it!");
      else if (guess>num)
        System.out.println ("That is too high.");
      else
        System.out.println ("That is too low.");
      end = IBIO.inputChar ("Exit game? (y/n)");
    }
  }
}

By the way, IBIO is a class provided by my IB program that we use to make Input/Output statements. 顺便说一下,IBIO是我的IB程序提供的一个类,我们用它来制作Input / Output语句。

This is IBIO.java: 这是IBIO.java:

    public class IBIO
{
    static void output (String info)
    {
 System.out.println (info);
    }


    static void output (char info)
    {
 System.out.println (info);
    }


    static void output (byte info)
    {
 System.out.println (info);
    }


    static void output (int info)
    {
 System.out.println (info);
    }


    static void output (long info)
    {
 System.out.println (info);
    }


    static void output (double info)
    {
 System.out.println (info);
    }


    static void output (boolean info)
    {
 System.out.println (info);
    }


    static String input (String prompt)
    {
 String inputLine = "";
 System.out.print (prompt);
 try
 {
     inputLine = (new java.io.BufferedReader (new java.io.InputStreamReader (System.in))).readLine ();
 }
 catch (Exception e)
 {
     String err = e.toString ();
     System.out.println (err);
     inputLine = "";
 }
 return inputLine;
    }


    static String inputString (String prompt)
    {
 return input (prompt);
    }


    static String input ()
    {
 return input ("");
    }


    static int inputInt ()
    {
 return inputInt ("");
    }


    static double inputDouble ()
    {
 return inputDouble ("");
    }


    static char inputChar (String prompt)
    {
 char result = (char) 0;
 try
 {
     result = input (prompt).charAt (0);
 }
 catch (Exception e)
 {
     result = (char) 0;
 }
 return result;
    }


    static byte inputByte (String prompt)
    {
 byte result = 0;
 try
 {
     result = Byte.valueOf (input (prompt).trim ()).byteValue ();
 }
 catch (Exception e)
 {
     result = 0;
 }
 return result;
    }


    static int inputInt (String prompt)
    {
 int result = 0;
 try
 {
     result = Integer.valueOf (input (prompt).trim ()).intValue ();
 }
 catch (Exception e)
 {
     result = 0;
 }
 return result;
    }


    static long inputLong (String prompt)
    {
 long result = 0;
 try
 {
     result = Long.valueOf (input (prompt).trim ()).longValue ();
 }
 catch (Exception e)
 {
     result = 0;
 }
 return result;
    }


    static double inputDouble (String prompt)
    {
 double result = 0;
 try
 {
     result = Double.valueOf (input (prompt).trim ()).doubleValue ();
 }
 catch (Exception e)
 {
     result = 0;
 }
 return result;
    }


    static boolean inputBoolean (String prompt)
    {
 boolean result = false;
 try
 {
     result = Boolean.valueOf (input (prompt).trim ()).booleanValue ();
 }
 catch (Exception e)
 {
     result = false;
 }
 return result;
    }
}

Sorry for the lengthy question. 很抱歉,这个冗长的问题。 Im new to Java. 我是Java新手。

The computer is doing exactly what you told it to. 计算机完全按照您的指示进行。 When GuessGame 's constructor runs: GuessGame的构造函数运行时:

  1. Declare end as a char local variable and initialise it to contain 'y' : end声明为char局部变量,并将其初始化为包含'y'

     char end = 'y'; 
  2. Run the loop body while end does not contain 'y' : end不包含'y'运行循环体:

     while (end!='y') 

    (since end does contain 'y' it does not run the loop body; it skips to the code after the loop). (因为end 确实包含'y'所以它运行循环主体;它在循环后跳至代码)。

The problem is that you will never enter the initial loop 问题是您永远不会进入初始循环

char end = 'y';
while (end!='y')

You instantiate end to y , then enter only if end is not y which will always be false, hence never enter the loop. 你实例endy ,那么只有当输入end是不是y这将永远是假的,因此不会进入循环。

Simply change the default value of end 只需更改end的默认值

char end = 'n';

Also, you don't have to cast the value 0 in your IBIO class 另外,您不必在IBIO类中强制转换值0

result = (char) 0;

You can simply do result = 0 and it will take the ASCII value. 您可以简单地将result = 0并将采用ASCII值。

I would also declare num and guess outside of the loop to avoid re-declaring them each time, as you did for end . 我还将声明num并在循环外进行guess ,以避免每次都像对end那样重新声明它们。

Finally, instead of declaring 7 output method with different paremeter type which simply do a System.out.println of the received parameter I would directly call System.out.println(value) . 最后,与其声明简单地对接收到的参数进行System.out.println的声明不同参数类型的7种output方法,不如直接调用System.out.println(value)

I would apply the same logic for all other methods that only call one method with the received parameter. 我将对仅使用接收的参数调用一个方法的所有其他方法应用相同的逻辑。

These two lines clearly contradict each other, the while loop will never execute. 这两行显然是相互矛盾的,而while循环将永远不会执行。 Initialize end to be a different value. 将end初始化为另一个值。

char end = 'y'; char end ='y';

while (end!='y') 一会儿(结束!='y')

You initialize the variable char end with value 'y'. 您使用值“ y”初始化变量char end。

char end = 'y';

Then the condition for your loop is 那么循环的条件是

while (end!='y')

This condition is never fulfilled, that's why it's out of the loop. 这个条件永远不会满足,这就是为什么它不循环。 Change the initial value of the variable end. 更改变量端的初始值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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