我正在创建一个子菜单,允许用户增加库存,减少库存并退出子菜单。我在为子菜单创建循环时遇到了一些问题。我正在使用do-while循环,但它并没有像我预期的那样工作。我使用布尔来控制循环,以及switch和case语句。我必须忽略代码中的一个小细节:
import java.util.*; //Scanner
public class tester2
{
public static void main (String []args)
{
int bookStock = 100; // Sets current bookStock amount to 100
boolean subMenuControl = false; // controls the loop for sub menu
do
{
System.out.println("\nMake your seletion from the choices below:\n"); //Prints this on screen to instruct user
System.out.println("A. Increase Stock Availability");
System.out.println("B. Reduce Stock Availability");
System.out.println("C. Quit\n");
System.out.print("Your choice? "); //Prompts user for a choice only A,B,C or a,b,c are valid
Scanner subMenuInput = new Scanner(System.in); //reads user choice from keyboard eg. A,B,C or a,b,c
String subMenuChoice = subMenuInput.nextLine(); //stores users selection into variable subMenuChoice
char subMenuAccess = subMenuChoice.charAt(0); //manipulates String into char
switch (subMenuAccess) //reads user input from variable and performs switch statement
{
case 'A': //Doesnt matter if user enters in lower or upper case
case 'a':
System.out.print("\nHow many books do you wish to add to stock: "); //prints to screen
Scanner increaseInput = new Scanner(System.in); //reads userinput
int increaseStockAmount = increaseInput.nextInt(); //stores userinut as interger
bookStock += increaseStockAmount; //increases bookStock by input amount
System.out.println("\nCurrent Stock is now: " + bookStock); // displays new bookstock amount
break; //exits case statement
case 'B': //Doesnt matter if user enters in lower or upper case
case 'b':
System.out.print("\nHow many books do you wish to reduce from stock: "); //prints to screen
Scanner reduceInput = new Scanner(System.in); //reads userinput
int reduceStockAmount = reduceInput.nextInt(); //stores userinut as interger
if (bookStock >= reduceStockAmount) //checks if user can reduce stock amount
{
bookStock -= reduceStockAmount; //reduces bookStock by input amount
System.out.println("\nCurrent Stock is now: " + bookStock); //displays new bookstock amount
}
else
{
System.out.println("\nCurrent Stock is " + bookStock + " can not reduce stock by " + reduceStockAmount); //displays to user that they can not reduce stock by entered amount
}
break; //exits case statement
case 'C': //Doesnt matter if user enters in lower or upper case
case 'c':
System.out.print("\nQuitting... ");
subMenuControl = true; //exits subMenu
break; //exits case statement
}
}
while (subMenuControl = false);
}
}
此程序的以下输出不会循环任何内容。任何建议都会非常有帮助。谢谢
我试过了(subMenuControl == false);在提出这个问题之前,它会返回错误。
程序运行但在提示用户输入后,它退出并且不再显示子菜单。我不希望它,除非...将添加subMenuControl = false;在每种情况下声明使它工作?
输出以下内容:
从以下选项中进行选择:
一个。增加库存可用性 B.减少库存可用性 C.退出
你的选择?一个
您希望将多少本书添加到库存中:7
现有股票现在:107
然后退出。
答案 0 :(得分:0)
您的问题是,您要将值false
分配给subMenuControl
while (subMenuControl = false);
这将始终评估为false。
如果您想要做的是重复while循环,只要subMenuControl
为false,您就会想要写
while (!subMenuControl);
!
是NOT运算符,因此它前面的任何布尔值都将计算为相反的值。在这种情况下,循环将重复,subMenuControl
为假,如果为真则结束。