我似乎无法弄清楚为什么我的addRecord()方法正在循环。具体而言,它仅在“输入销售ID>>”处循环在方法的第一个if / else中。我试过在代码末尾调用programMenu()方法,但编译器只是继续尝试运行它们。此方法应仅在switch case语句中接收到“a”时执行,但在输入“a”和“c”时运行,然后在“Enter Sales ID>>”处继续循环到相同的else语句。 ”。
我的方法如下:
putArrayToDb = (tableName, arrayToPut) => {
if (this.dbPromise) {
this.dbPromise.then(db => {
if (!db) return;
var tx = db.transaction(tableName, 'readwrite');
var store = tx.objectStore(tableName);
arrayToPut.map(arrayItem => {
store.put(arrayItem);
});
})
}
}
putArrayToDb('categories', ["value1", "value2", "value3"]);
我的开关案例如下:
public static int addRecord(Salesperson[]salesPeopleArray, int
numOfSalesPpl)
{
String name=null;
String idNum;
double salesAmt = 0;
String response;
numOfSalesPpl = 0;
final int MAX_LIMIT=20;
final int ID_NUM_LIMIT = 8;
if(numOfSalesPpl == MAX_LIMIT)
{
System.out.print("Database has reached capacity.");
System.out.print(" Please delete a record before ");
System.out.println("adding to the database.");
}
else
System.out.print("Enter Sales ID:>> ");
idNum = userInput.nextLine();
if(idNum.length() != ID_NUM_LIMIT)
{
System.out.println(">>>>>Sales ID must be 8 digits<<<<<<");
programMenu();
}
else
{
System.out.print("Please enter name: >> ");
name = userInput.nextLine();
System.out.print("Sales amount : >> ");
salesAmt = userInput.nextDouble();
salesPeopleArray[numOfSalesPpl] = new Salesperson(name,
idNum,salesAmt);
++numOfSalesPpl;
userInput.nextLine();
System.out.print("Do you want to display database> Y/N >>");
response = userInput.nextLine();
if(response.equals("y"))
displayDatabase(salesPeopleArray, numOfSalesPpl);
}
return numOfSalesPpl;
}
我的programMenu()方法如下所示:
String selection = programMenu();
boolean loop = true;
do
{
switch(selection)
{
case "A":
case "a":
addRecord(salesPeopleArray, numOfSalesPpl);
break;
case "C":
case "c":
changeRecord(numOfSalesPpl, salesPeopleArray);
break;
case "Q":
case "q":
System.out.print("You Are Leaving Database");
System.out.print(" ");
loop = false;
break;
}
}
while(loop);
}
答案 0 :(得分:0)
这里你的代码只有一次获得用户输入(“A / a”)然后进入循环因为“selection”in已经值“A”所以。当你 将此行“ String selection = programMenu(); ”放在循环内,循环时间询问用户输入,您可以更改switch case语句。
boolean loop = true;
do
{
String selection = programMenu();
switch(selection)
{
case "A":
case "a":
addRecord(salesPeopleArray, numOfSalesPpl);
break;
case "C":
case "c":
changeRecord(numOfSalesPpl, salesPeopleArray);
break;
case "Q":
case "q":
System.out.print("You Are Leaving Database");
System.out.print(" ");
loop = false;
break;
}
}
while(loop);
}