0

我试图弄清楚如何从用户那里获取输入并将其存储到一个数组中。我不能使用arrayList(简单的方法哈哈),而是使用标准array[5]。我在这里和谷歌上看过,由于问题和回复的数量庞大,我还没有找到一个好的答案。使用扫描仪获取输入不是问题。将输入存储在数组中不是问题。我遇到的问题是我需要一次存储一个输入。

目前我正在使用 for 循环来收集信息,但它想一次收集整个数组。

    for (int i=0;i<5;i++)
                array[i] = input.nextInt();

    for (int i=0;i<array.length;i++)
                System.out.println(array[i]+" ");

我一直在搞乱它,但是如果我删除第一个 for 循环,我不确定在 array[] 括号中放什么。BlueJ 只是说“array[] 不是一个声明”

如何一次只接受一个值并让用户确定他们是否要执行下一个?

这是一个家庭作业,但家庭作业是关于创建一个带有字符串命令的控制台,这是我需要理解的一个概念,以完成项目的其余部分工作正常。

4

3 回答 3

1
boolean c = true;                               
    Scanner sc=new Scanner(System.in);
    int arr[] = new int[5];
    int i =0;
    int y = 1;
    while(c){
        System.out.println("Enter "+i+" index of array: ");
        arr[i]=sc.nextInt();
        i++;
        System.out.println("Want to enter more if yes press 1 or press 2 ");
        y = sc.nextInt();
        if(y==1)c=true;
        else c=false;

    }
于 2013-09-10T06:09:12.040 回答
0

这条路怎么样?

   Scanner sc=new Scanner(System.in);
   int[] arr=new int[5];
    int i=0;
   while (i<arr.length){
       System.out.println("Enter "+i+" index of array: ");
       arr[i]=sc.nextInt();
       i++;
   }
于 2013-09-10T05:52:56.113 回答
0

将此用作参考实现。关于如何设计简单控制台程序的一般指示。我目前正在使用 JDK 6。如果您使用的是 JDK 7,则可以使用switch case字符串而不是if-else.

public class Stack {

    int[] stack; // holds our list
    int MAX_SIZE, size; /* size helps us print the current list avoiding to 
                           print zer0es & the ints deleted from the list */    
    public Stack(int i) {
        MAX_SIZE = i; // max size makes the length configurable
        stack = new int[MAX_SIZE]; // intialize the list with zer0es
    }

    public static void main(String[] args) throws IOException {
        new Stack(2).run(); // list of max length 2
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        // enter an infinite loop
        while (true) {
            showMenu(); // show the menu/prompt after every operation

            // scan user response; expect a 2nd parameter after a space " "
            String[] resp = scanner.nextLine().split(" "); // like "add <n>"
            // split the response so that resp[0] = add|list|delete|exit etc.
            System.out.println(); // and resp[1] = <n> if provided

            // process "add <n>"; check that "<n>" is provided
            if ("add".equals(resp[0]) && resp.length == 2) {
                if (size >= MAX_SIZE) { // if the list is full
                    System.out.print("Sorry, the list is full! ");
                    printList(); // print the list
                    continue; // skip the rest and show menu again
                }
                // throws exception if not an int; handle it
                // if the list is NOT full; save resp[1] = <n>
                // as int at "stack[size]" and do "size = size + 1"
                stack[size++] = Integer.parseInt(resp[1]);
                printList(); // print the list

            // process "list"
            } else if ("list".equals(resp[0])) {
                printList(); // print the list

            // process "delete"
            } else if ("delete".equals(resp[0])) {
                if (size == 0) { // if the list is empty
                    System.out.println("List is already empty!\n");
                    continue; // skip the rest and show menu again
                }
                // if the list is NOT empty just reduce the
                size--; // size by 1 to delete the last element
                printList(); // print the list

            // process "exit"
            } else if ("exit".equals(resp[0])) {
                break; // from the loop; program ends

            // if user types anything else
            } else {
                System.out.println("Invalid command!\n");
            }
        }
    }

    private void printList() {
        System.out.print("List: {"); // print list prefix

        // print only if any ints entered by user
        if (size > 0) { // are available i.e. size > 0
            int i = 0;
            for (; i < size - 1; i++) {
                System.out.print(stack[i] + ",");
            }
            System.out.print(stack[i]);
        }
        System.out.println("}\n"); // print list suffix
    }

    private void showMenu() {
      System.out.println("Enter one of the following commands:");
      // Check String#format() docs for how "%" format specifiers work
      System.out.printf(" %-8s: %s\n", "add <n>", "to add n to the list");
      System.out.printf(" %-8s: %s\n", "delete", "to delete the last number");
      System.out.printf(" %-8s: %s\n", "list", "to list all the numbers");
      System.out.printf(" %-8s: %s\n", "exit", "to terminate the program");
      System.out.print("$ "); // acts as command prompt
    }
}

样品运行

Enter one of the following commands:
 add <n> : to add n to the list
 delete  : to delete the last number
 list    : to list all the numbers
 exit    : to terminate the program
$ list

List: {}

Enter one of the following commands:
 add <n> : to add n to the list
 delete  : to delete the last number
 list    : to list all the numbers
 exit    : to terminate the program
$ add 1

List: {1}

Enter one of the following commands:
 add <n> : to add n to the list
 delete  : to delete the last number
 list    : to list all the numbers
 exit    : to terminate the program
$ add 2

List: {1,2}

Enter one of the following commands:
 add <n> : to add n to the list
 delete  : to delete the last number
 list    : to list all the numbers
 exit    : to terminate the program
$ add 3

Sorry, the list is full! List: {1,2}

Enter one of the following commands:
 add <n> : to add n to the list
 delete  : to delete the last number
 list    : to list all the numbers
 exit    : to terminate the program
$ delete

List: {1}

Enter one of the following commands:
 add <n> : to add n to the list
 delete  : to delete the last number
 list    : to list all the numbers
 exit    : to terminate the program
$ delete

List: {}

Enter one of the following commands:
 add <n> : to add n to the list
 delete  : to delete the last number
 list    : to list all the numbers
 exit    : to terminate the program
$ delete

List is already empty!

Enter one of the following commands:
 add <n> : to add n to the list
 delete  : to delete the last number
 list    : to list all the numbers
 exit    : to terminate the program
$ exit

(实话实说:我越来越无聊了。所以,我写了它,并认为还不如把它贴出来。)

于 2013-09-10T07:16:30.167 回答