如何在java中使用if-else语句获取多个用户输入。
我创建了一个菜单,要求用户输入一个字母,程序将显示他们选择的命令选项。现在我有了它,所以如果用户输入“a”,那么“输入三个整数来初始化表”就会打印出来。我需要它然后打印“请输入一个命令”下一步,但它只是不断打印“输入三个整数来初始化表”我已经尝试了一段时间不同的方法,现在我不知道该怎么做。有什么帮助吗?您需要循环询问三个整数。最好使用数组来捕获值。像这样的事情:如何在java中使用if-else语句获取多个用户输入。,java,if-statement,menu,statements,Java,If Statement,Menu,Statements,我创建了一个菜单,要求用户输入一个字母,程序将显示他们选择的命令选项。现在我有了它,所以如果用户输入“a”,那么“输入三个整数来初始化表”就会打印出来。我需要它然后打印“请输入一个命令”下一步,但它只是不断打印“输入三个整数来初始化表”我已经尝试了一段时间不同的方法,现在我不知道该怎么做。有什么帮助吗?您需要循环询问三个整数。最好使用数组来捕获值。像这样的事情: import java.util.Scanner; public class Assignment6 { public stati
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"));
{
}
}
}
// … more if-else statements
input = in.nextLine(); // <-- add new user input
} while (!input.equals("q"));
if(input.equals(“a”))
{
System.out.println(“a[输入三个整数来初始化一个表:”);
int newTable[]=新int[3];
对于(int i=0;i<3;i++){
System.out.println(“输入”+(i+1)+“表值:”);
newTable[i]=in.nextInt();
}
}
查看边做边做循环的底部:
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();
}
}
它会继续打印该语句,因为在if-else语句完成后,input
的值仍然等于“a”,因为您没有更改input
的值
也许您希望用户在while循环再次检查之前输入新的input
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=in.nextLine();// 由于根据用户输入的命令,您接受用户输入的数量会发生变化,因此请尝试在if或else条件中使用循环。对于输入的任何命令,do-while循环只会保持相同的次数。试着这样做:
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"));
{
}
}
}
// … more if-else statements
input = in.nextLine(); // <-- add new user input
} while (!input.equals("q"));
字符串输入=”;
做{
System.out.println(“请输入命令:”);
input=in.nextLine();
if(input.equals('a')){
System.out.println(“a[输入三个整数来初始化一个表:”);
int newTable[]=新int[3];
对于(int i=0;i<3;i++){
System.out.println(“输入”+(i+1)+“表值:”);
newTable[i]=in.nextInt();
}
}
else if(输入.等于('b')){
//接受命令“b”所需的任意数量的输入
}
}而(!input.equals('q'))
外部do while循环将确保在用户输入“q”之前,您可以接受来自用户的尽可能多的命令。如果您希望用户为您提供另一个输入
,请将输入输入
的语句放在循环内部,而不是外部。嘿,谢谢威尔·牛顿,这正是我一直坚持的。它现在工作得很好。