【发布时间】:2013-01-25 19:15:33
【问题描述】:
我正在尝试创建一个简单的控制台程序,询问用户是否要创建列表,如果“是”,则允许他们输入列表的名称。然后它应该在退出程序之前“打印”列表的名称。
我的代码允许用户在第一部分说y 或n,但在我的条件语句中它不允许用户输入列表的名称;它只是完成了程序。没有错误信息;它只是没有按我的预期运行。这是我的代码:
public static void main(String[] args) throws IOException
{
getAnswers();
}
public static void getAnswers()throws IOException{
char answer;
String listName;
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
System.out.println ("Would like to create a list (y/n)? ");
answer = (char) br.read();
***if (answer == 'y'){
System.out.println("Enter the name of the list: ");
listName = br.readLine();
System.out.println ("The name of your list is: " + listName);}***
//insert code to save name to innerList
else if (answer == 'n'){
System.out.println ("No list created, yet");
}
//check if lists exist in innerList
// print existing classes; if no classes
// system.out.println ("No lists where created. Press any key to exit")
}
提前感谢您在这方面的时间和帮助!
【问题讨论】:
-
我开始尝试进行一些调试,但对于我的可变“字符串列表名称”,它不能被识别为变量;因此没有任何东西被传递给它。当我将鼠标悬停在 listName 上时,会说“listName =>”listName”在当前上下文中不是已知变量。
标签: java console-application bufferedreader conditional-statements