2014-04-04 24 views
-1
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”,那么将输出“输入三个整数来初始化表格”。我需要它然后打印“请输入命令”接下来,但它只是不断打印“输入三个整数itialize表”我一直在尝试不同的方法一段时间了,我不知道该怎么做。任何帮助?如何在java中使用if-else语句获取多个用户输入。

+0

如果你想让用户给你另一个'输入',把输入'input'的语句放到循环中,而不是外部。 – ajb

回答

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阿赫桑已经说你需要首先if语句中添加更多的代码来打印你提到的其他命令。

+0

嘿,谢谢你,牛顿,那正是我被卡住了。它现在正在完美工作。 – mrcj1208

0

你需要循环询问三个整数。更好地使用数组来捕获值。 Somethign这样的:

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(); 
    } 

} 
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')) 

外做while循环将确保你可以从用户,直到用户输入“Q”接受多个命令。

相关问题