[英]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
的構造函數運行時:
將end
聲明為char
局部變量,並將其初始化為包含'y'
:
char end = 'y';
在end
不包含'y'
運行循環體:
while (end!='y')
(因為end
確實包含'y'
所以它不運行循環主體;它在循環后跳至代碼)。
問題是您永遠不會進入初始循環
char end = 'y';
while (end!='y')
你實例end
至y
,那么只有當輸入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.