import java.util.Scanner;
public class Assignment6 {
public static void commandList()
{
System.out.println("Command Options------------");
System.out.println("a: Create a new table");
System.out.println("b: Change the row sizes");
System.out.println("c: Change the column sizes");
System.out.println("d: Change the data types");
System.out.println("e: Change the formats");
System.out.println("f: Display the table");
System.out.println("g: Display the log data");
System.out.println("?: Display this menu");
System.out.println("q: Quit the program");
System.out.println("---------------------------");
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Command Options------------");
System.out.println("a: Create a new table");
System.out.println("b: Change the row sizes");
System.out.println("c: Change the column sizes");
System.out.println("d: Change the data types");
System.out.println("e: Change the formats");
System.out.println("f: Display the table");
System.out.println("g: Display the log data");
System.out.println("?: Display this menu");
System.out.println("q: Quit the program");
System.out.println("---------------------------");
String input = "";
System.out.println("Please input a command:");
input = in.nextLine();
do
{
if (input.equals("a"))
{
System.out.println("a [Input three integers to ititialze a table:] ");
int newTable = in.nextInt();
}
else if (input.equals("b"))
{
System.out.println("Change the row sizes");
int newRow = in.nextInt();
}
else if (input.equals("c"))
{
System.out.println("c [Type an integer to update the table column]: ");
int newColumn = in.nextInt();
}
else if (input.equals("d"))
{
System.out.println("d [Type an integer to update the data type]: ");
int newDataType = in.nextInt();
}
else if (input.equals("e"))
{
System.out.println("e [Type and integer to update printf format]: ");
int newPrintf = in.nextInt();
}
else if (input.equals("f"))
{
System.out.println("f [Display the table]");
}
else if (input.equals("g"))
{
System.out.println("g [Display the log data]");
}
else if (input.equals("?"))
{
commandList();
}
else
{
System.out.println("Invalid ***Type ? to get commands***");
}
}
while (!input.equals("q"));
{
}
}
}
我创建了一个菜单,我要求用户输入一个字母,程序将显示他们选择的命令选项。现在我有了它,所以如果用户输入“a”然后将打印“输入三个整数来初始化表”。我需要它然后打印“请输入一个命令”接下来,但它只是继续打印“输入三个整数来表示我的表”我一直在尝试不同的方法一段时间,我不知道该怎么做。有什么帮助吗?
答案 0 :(得分:0)
你需要循环以询问三个整数。更好地使用数组来捕获值。有点像这样:
if (input.equals("a"))
{
System.out.println("a [Input three integers to ititialze a table:] ");
int newTable[] = new int[3];
for (int i = 0; i < 3 ; i++) {
System.out.println("Input " + (i+1) + "table value: ");
newTable[i] = in.nextInt();
}
}
答案 1 :(得分:0)
查看do-while
循环的底部:
do
{
if (input.equals("a"))
{
System.out.println("a [Input three integers to ititialze a table:] ");
int newTable = in.nextInt();
}
// … more if-else statements
} while (!input.equals("q"));
它继续打印该语句,因为在if-else语句结束后,input
的值仍然等于“a”,因为您没有更改input
的值。
也许您希望用户在while循环再次检查之前输入新的input
。
// … more if-else statements
input = in.nextLine(); // <-- add new user input
} while (!input.equals("q"));
正如Juned Ahsan已经说过你需要在第一个if
语句中添加更多代码来打印你提到的其他命令。
答案 2 :(得分:0)
由于取决于用户输入的命令,您从用户接受的输入数量会发生变化,请尝试在if或else条件中使用循环。对于输入的任何命令,do while循环将保持相同的次数。尝试这样的事情:
String input = "";
do {
System.out.println("Please input a command:");
input = in.nextLine();
if (input.equals('a')) {
System.out.println("a [Input three integers to ititialze a table:] ");
int newTable[] = new int[3];
for (int i = 0; i < 3 ; i++) {
System.out.println("Input " + (i+1) + "table value: ");
newTable[i] = in.nextInt();
}
}
else if(input.equals('b')) {
// Accept as many inputs you want for command 'b'
}
} while (!input.equals('q'))
外部do while循环将确保您可以接受来自用户的任意数量的命令,直到用户输入“q”。