繁体   English   中英

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

[英]Java code for guessing game not printing anything

因此,我在学校开设了一门计算机科学课程,我们在其中学习Java。 我们被分配去做一个简单的基于文本的猜谜游戏。 到现在为止,我已经完成了它,但是由于运行内核时没有打印任何内容,因此我似乎找不到混乱的地方。

这是代码:

    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)");
    }
  }
}

顺便说一下,IBIO是我的IB程序提供的一个类,我们用它来制作Input / Output语句。

这是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;
    }
}

很抱歉,这个冗长的问题。 我是Java新手。

计算机完全按照您的指示进行。 GuessGame的构造函数运行时:

  1. end声明为char局部变量,并将其初始化为包含'y'

     char end = 'y'; 
  2. end不包含'y'运行循环体:

     while (end!='y') 

    (因为end 确实包含'y'所以它运行循环主体;它在循环后跳至代码)。

问题是您永远不会进入初始循环

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

你实例endy ,那么只有当输入end是不是y这将永远是假的,因此不会进入循环。

只需更改end的默认值

char end = 'n';

另外,您不必在IBIO类中强制转换值0

result = (char) 0;

您可以简单地将result = 0并将采用ASCII值。

我还将声明num并在循环外进行guess ,以避免每次都像对end那样重新声明它们。

最后,与其声明简单地对接收到的参数进行System.out.println的声明不同参数类型的7种output方法,不如直接调用System.out.println(value)

我将对仅使用接收的参数调用一个方法的所有其他方法应用相同的逻辑。

这两行显然是相互矛盾的,而while循环将永远不会执行。 将end初始化为另一个值。

char end ='y';

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

您使用值“ y”初始化变量char end。

char end = 'y';

那么循环的条件是

while (end!='y')

这个条件永远不会满足,这就是为什么它不循环。 更改变量端的初始值。

暂无
暂无

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

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