我的代码编译,但是当我尝试运行它时,它在我运行Y / N循环控制变量时给出了“未定义名称”的错误。我输入了以下整个程序的编码。
此外,它在询问循环问题时显示关闭对话框..我在这里做错了什么?
//declarations
String itemName; //name of item
String itemPriceInput; //user input of price
double itemPrice; //parsed name of price
String itemQuantityInput; //user input of quantity
double itemQuantity; //parsed input of quantity
String inputMore; //loop control
//Create file instance
java.io.File itemAttributes = new java.io.File("Items_In_Stock.txt");
//Create a scanner for the file
Scanner input = new Scanner(itemAttributes);
System.out.println("Welcome to the inventory list program"); //welcome dialog for user
System.out.println("This program will show items on hand, as well as their info"); //more dialog
System.out.println("Enter 'Y' to proceed, or 'Q' to quit"); //asking to start program
inputMore=input.next(); //taking input for loop control
while (inputMore.equalsIgnoreCase("y")) //beginning outer loop
{
while (input.hasNext()) //beginning inner loop
{
itemName=input.next(); // input item name
System.out.print (" ");
itemPriceInput=input.next(); //input item price
itemPrice=Double.parseDouble(itemPriceInput); //parse to double
System.out.print (" ");
itemQuantityInput=input.next(); //input quantity
itemQuantity=Double.parseDouble(itemQuantityInput); //parse to double
System.out.println("Name: " + itemName + " " + "Price: " + itemPrice + " " + "Quantity: " + itemQuantity); //display input items
} //close inner loop
System.out.print("Enter 'Y' to input more, or 'Q' to quit"); //asking user to input more, or quit
} //close outer loop
input.close(); //close program
System.out.println("Thank you for using the program. Goodbye");//goodbye statement
答案 0 :(得分:1)
我假设您的txt
文件包含
ddda 8998 787
gdfgd 8998 787
到目前为止我可以看到两个漏洞:
当您询问用户"Enter 'Y' to proceed, or 'Q' to quit"
时,您应使用System.in
为此进行不同的扫描。
为什么你在里面使用while循环?你应该在那里使用if
条件来要求用户选择..
这是更新后的代码。它将解决您的问题:
// declarations
String itemName; // name of item
String itemPriceInput; // user input of price
double itemPrice; // parsed name of price
String itemQuantityInput; // user input of quantity
double itemQuantity; // parsed input of quantity
String inputMore; // loop control
// Create file instance
java.io.File itemAttributes = new java.io.File("Items_In_Stock.txt");
// Create a scanner for the file
Scanner input = new Scanner(itemAttributes);
System.out.println("Welcome to the inventory list program");
System.out
.println("This program will show items on hand, as well as their info");
System.out.println("Enter 'Y' to proceed, or 'Q' to quit");
// inputMore = input.next(); // taking input for loop control
Scanner sc = new Scanner(System.in);
// System.out.println(inputMore);
while (sc.next().equalsIgnoreCase("y")) // beginning outer loop
{
if(input.hasNext()) // beginning inner loop
{
itemName = input.next(); // input item name
itemPriceInput = input.next(); // input item price
itemPrice = Double.parseDouble(itemPriceInput); // parse to
// double
itemQuantityInput = input.next(); // input quantity
itemQuantity = Double.parseDouble(itemQuantityInput); // parse
// to
// double
System.out.println("Name: " + itemName + " " + "Price: "
+ itemPrice + " " + "Quantity: " + itemQuantity); // display
// input
// items
} // close inner loop
else
{
System.out.println("No more records");
break;
}
System.out.print("Enter 'Y' to input more, or 'Q' to quit"); // asking
// user
// to
// input
// more,
// or
// inputMore = input.next(); // quit
} // close outer loop
input.close(); // close program
System.out.println("Thank you for using the program. Goodbye");// goodbye
// statement}
答案 1 :(得分:0)
我看到的唯一错误是
while (inputMore.equalsIgnoreCase("y"))
您忘记在循环结束时更新inputMore,换句话说,inputMoreinputMore.equalsIgnoreCase("y")
将始终返回true,因为您永远不会更改它的值。
您可以更改为
if (inputMore.equalsIgnoreCase("y"))
或在循环结束时添加另一个
inputMore = input.hext();