获取用户输入并将其存储到数组中,然后将另一个用户输入与数组进行比较以检查重复的答案

时间:2015-08-03 02:33:15

标签: java arrays for-loop

我是Java编程的新手。我基本上做的是要求用户选择然后显示输出。但是,如果用户已经选择了相同的输出,系统应该能够给出一条消息,然后再次返回循环。谢谢!这就是我到目前为止所做的:

    BufferedReader br = new BufferedReader (new InputStreamReader(System.in));

    String[] array1 = {"A","B","C","D"};

    for(int a=0; a<array1.length; a++)
    {
        System.out.print("\n Pick an exception: ");
        System.out.print("\n A. Arithmetic Exception");
        System.out.print("\n B. Array Out Of Bounds Exception");
        System.out.print("\n C. Array Store Exception");
        System.out.print("\n D. Number Format Exception");
        System.out.print("\n\nPick: ");
        pick = br.readLine();

        if(array1[a].equalsIgnoreCase ("a"))
        {
            //output1;
        }

        else if(array1[a].equalsIgnoreCase ("b"))
        {
            //output2;
        }
        else if(array1[a].equalsIgnoreCase ("c"))
        {
            ////output3;
        }
        else if(array1[a].equalsIgnoreCase ("d"))
        {
            //output4;
        }


        // PS If anyone can also let me know how I can display the user input depending to the sequence it was selected what would be great!

        System.out.print("\n\n Status: " +array1[a] + "\n\n ");
    }
}

1 个答案:

答案 0 :(得分:0)

好的,让我们来看看您的原始代码。

    String[] array1 = {"A","B","C","D"};
    String pick = "";

    for(int a=0; a<array1.length; a++)
    {
        System.out.print("\n Pick an exception: ");
        System.out.print("\n A. Arithmetic Exception");
        System.out.print("\n B. Array Out Of Bounds Exception");
        System.out.print("\n C. Array Store Exception");
        System.out.print("\n D. Number Format Exception");
        System.out.print("\n\nPick: ");
        pick = br.readLine();
        pick = array1[a]

        if(array1[a].equalsIgnoreCase ("a"))
        {
            //output1;
        }

        else if(array1[a].equalsIgnoreCase ("b"))
        {
            //output2;
        }
        else if(array1[a].equalsIgnoreCase ("c"))
        {
            ////output3;
        }
        else if(array1[a].equalsIgnoreCase ("d"))
        {
            //output4;
        }


        // PS If anyone can also let me know how I can display the user input depending to the sequence it was selected what would be great!

        System.out.print("\n\n Status: " +array1[a] + "\n\n ");
    }
}

首先,除非你有其他原因,否则没有特别用的说法

System.out.print("\n some text");

您可以改为使用

System.out.println("some text");

转到下一期。

执行System.out.print后,您写了

pick = br.readLine();
pick = array1[a];

首先,阅读的重点是什么?您将pick设置为用户输入,但随后将pick设置为array1[a]。我想你想说

array[a] = br.readLine();

现在很长一段时间。这个(非常)修订版的代码应该有效:

package pack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

    public class Test {
        public static void main(String[] args) {
            BufferedReader in = new BufferedReader(new     InputStreamReader(System.in));

        String[] array1 = new String[4];

        for (int a = 0; a < array1.length; a++) {
            System.out.print("\n Pick an exception: ");
            System.out.print("\n A. Arithmetic Exception");
            System.out.print("\n B. Array Out Of Bounds Exception");
            System.out.print("\n C. Array Store Exception");
            System.out.print("\n D. Number Format Exception");
            System.out.print("\n\nPick: ");
            try {
                array1[a] = in.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean a = false;
        boolean b = false;
        boolean c = false;
        boolean d = false;

        for (String i : array1) {
            if (i.equals("a")) {
                if(a)
                    System.err.println("Repeat!");
                a = true;
            } else if (i.equals("b")) {
                if(b)
                    System.err.println("Repeat!");
                b = true;
            } else if (i.equals("c")) {
                if(c)
                    System.err.println("Repeat!");
                c = true;
            } else {
                if(d)
                    System.err.println("Repeat!");
                d = true;
            }
        }
    }
}

效率非常低,但它会做到。