简体   繁体   English

如何将用户输入分成两个单独的数组?

[英]How do I get user input into two separate arrays?

Hi I am having trouble with Scanner to get user input two separate ArrayList . 嗨,我在使用Scanner获取用户输入两个单独的ArrayList时遇到了麻烦。 When I run this code I get an IndexOutOfBounds exception after entering the two arrays. 当我运行此代码时,输​​入两个数组后,我得到一个IndexOutOfBounds异常。 The code adds two binary numbers together using logic of a ripple adder. 该代码使用纹波加法器的逻辑将两个二进制数字相加。 An example of intended user input would be 预期的用户输入的示例是

Enter A array: 1 0 1 0 Enter B Array: 0 0 0 1 producing: 1 0 1 1 输入A数组:1 0 1 0输入B数组:0 0 0 1产生:1 0 1 1

The code works when arrays are hard coded, how can I get the user to enter the arrays? 当对数组进行硬编码时,代码可以工作,如何让用户输入数组?

Code is shown below 代码如下所示

import java.util.*;

public class AdderApp {
    public static void main(String[] args) {

        Scanner inputA = new Scanner(System.in);
        ArrayList<Integer> aList = new ArrayList<Integer>();
        ArrayList<Integer> bList = new ArrayList<Integer>();
        int c = 0;

        System.out.println("Enter A array");
        aList.add(inputA.nextInt());

        Scanner inputB = new Scanner(System.in);
        System.out.println("Enter B array");
        bList.add(inputB.nextInt());

        Adder bit1 = new Adder(parseInput(aList.get(3)), parseInput(bList.get(3)), parseInput(c));
        Adder bit2 = new Adder(parseInput(aList.get(2)), parseInput(bList.get(2)), bit1.getCout());
        Adder bit3 = new Adder(parseInput(aList.get(1)), parseInput(bList.get(1)), bit2.getCout());
        Adder bit4 = new Adder(parseInput(aList.get(0)), parseInput(bList.get(0)), bit3.getCout());

        if (bit4.getCout() == false) {
            System.out.println(bit4.toString() + " " + bit3.toString() + " " + bit2.toString() + " " + bit1.toString());
        } else {
            System.out.println("overflow!");
        }

    }

    public static boolean parseInput(int i) {

        if (i == 1) {
            return true;
        } else {
            return false;
        }

    }
}

Code for Adder class: 加法器类的代码:

    public class Adder {

    private boolean a, b, cin, cout, s;

    /**
     * Full Adder contructor
     */

    public Adder(boolean a, boolean b, boolean cin) {

        this.a = a;
        this.b = b;
        this.cin = cin;

        s = nand(nand(a, b), cin); //sum bit
        cout = or(and(nand(a, b), cin), and(a, b)); // - carry bit

    }

    /** Half adder constructor */

    //    public Adder (bloolean a, boolean b) {
    //
    //      this.a = a;
    //      this.b = b;
    //
    //      s = 
    //}

    /**
     * NAND gate
     */
    public boolean nand(boolean a, boolean b) {

        return a ^ b;

    }

    /**
     * AND gate
     */
    public boolean and(boolean a, boolean b) {

        return a && b;

    }

    /**
     * OR gate
     */
    public boolean or(boolean a, boolean b) {

        return a || b;

    }

    public boolean getCout() {

        return cout;

    }

    public String toString() {

        if (s == true) {
            return "1";
        } else {
            return "0";
        }

    }

    public String toStringCout() {

        if (cout == true) {
            return "1";
        } else {
            return "0";
        }

    }
}

Scanner.nextInt gets the next integer in the input, and then stops. Scanner.nextInt获取输入中的下一个整数,然后停止。 Each of your lists only contains 1 element. 您的每个列表仅包含1个元素。

Use something along these lines instead: 改用以下方式:

String[] input = inputA.nextLine().split(" ");
for (String s : input)
{
    try { aList.add(Integer.parseInt(s)); }
    catch(NumberFormatException nfe) { /* handle exception as desired */ }
}

Alternatively, you should be able to use something like: 另外,您应该可以使用类似:

while (inputA.hasNextInt())
{
    aList.add(inputA.nextInt());
}

Your entire AdderApp class can be simplified and improved to accept any bit length by accepting the input in a slightly different way and then using a for loop to add each bit. 通过以略有不同的方式接受输入,然后使用for循环添加每个位,可以简化和改进整个AdderApp类以接受任何位长。 The parseInput function can be replaced with a simple boolean comparison: parseInput函数可以替换为简单的布尔比较:

import java.util.*;

public class AdderApp {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter A array");
        char[] aIn = input.nextLine().replace(" ", "").toCharArray();
        System.out.println("Enter B array");
        char[] bIn = input.nextLine().replace(" ", "").toCharArray();

        StringBuilder result = new StringBuilder();        
        Adder bit = new Adder(false, false, false);

        for (int i = aIn.length - 1; i >= 0; --i) {
            bit = new Adder((aIn[i] == '1'), (bIn[i] == '1'), bit.getCout());
            result.append(bit + " ");
        }
        System.out.println(bit.getCout() ? "overflow!" : result.reverse());
    }
}

The user should input 4 numbers, your one just allow the user to enter 1 number: 用户应输入4个数字,而您只允许用户输入1个数字:

int count = 0;
Scanner inputA = new Scanner(System.in);
System.out.println("Enter A array");
while(count < 4){
  count++;
  aList.add(inputA.nextInt());
}
count = 0;

Scanner inputB = new Scanner(System.in);
System.out.println("Enter B array");
while(count < 4){
  count++;
  bList.add(inputB.nextInt());
}

If you want to use hasNextInt() : 如果要使用hasNextInt()

while(inputA.hasNextInt()){
  count ++;
  aList.add(inputA.nextInt());
  if(count == 4){
    count = 0;
    break;
  }
}

You should be having a for loop to have an input into your ArrayList. 您应该有一个for循环,可以将输入输入到ArrayList中。

    System.out.println("Enter A array");
    for (int i = 0; i < 4; i++) {
        aList.add(inputA.nextInt());
    }


    Scanner inputB = new Scanner(System.in);

    System.out.println("Enter B array");           
    for (int i = 0; i < 4; i++) {
        bList.add(inputB.nextInt());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM