我的代码可以编译,但是当我尝试运行它时,当我运行 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
最佳答案
我假设您的 txt
文件包含类似
ddda 8998 787
gdfgd 8998 787
到目前为止,我可以看到两个缺陷:
当您要求用户 “输入‘Y’继续,或输入‘Q’退出”
时,您应该使用 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}
关于java - Undefined Name "Y",以及奇怪的输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47256389/