扫描仪不会扫描第二行

时间:2017-01-15 18:47:17

标签: java arrays if-statement while-loop

我有这个代码,在读完前三行输入后,它会终止程序,不会让我进入下一行。这是代码:

import java.util.Arrays;
import java.util.Scanner;
public class cycle {
public static void main(String[] arg)
{
    System.out.println("Put in numbers");
    Scanner in=new Scanner(System.in);
    int indicator=Integer.parseInt(in.nextLine());
    if(indicator==1)
    {
        mission1();
    }
    else if(indicator==2)
    {
        mission2();
    }
    in.close();
}
static void mission1()
{
    Scanner miss1=new Scanner(System.in);
    int citizens=Integer.valueOf(miss1.nextLine());
    String lines=miss1.nextLine();
    lines=lines.replaceAll("\\s", "");
    int length=lines.length();
    String lines2=miss1.nextLine();
    lines2=lines.replaceAll("\\s", "");
    int length2=lines.length();
    while(citizens!=length||citizens!=length2)
    {
        System.out.println("Citizens number do not match, try again" );
        miss1=new Scanner(System.in);
        citizens=Integer.valueOf(miss1.nextLine());
        lines=miss1.nextLine();
        lines=lines.replaceAll("\\s", "");
        length=lines.length();
        lines2=miss1.nextLine();
        lines2=lines.replaceAll("\\s", "");
        length2=lines.length();
        miss1.close();
        if(citizens!=length||citizens!=length2)
        {
            throw new IndexOutOfBoundsException("Numebr of citizens do not     match. Please enter numbers again");
        }
        else if(citizens==length&&citizens==length2)
        {
            String[] strs=lines.trim().split("\\s");
            length=lines.length();
            int[] dspeed = new int[length];
            for(int i=0; i<length;i++)
            {
                dspeed[i]=Integer.parseInt(strs[i]);
            }   
            String[] strs2=lines2.trim().split("\\s+");
            int[] pspeed = new int[length2];
            for(int i=0; i<length2;i++)
            {
                pspeed[i]=Integer.parseInt(strs2[i]);
            }
            Arrays.sort(dspeed);

            break;
        }
    }


    }
static void mission2()
{

}
}

例如,输入如下:

Put in numbers
1
3
1 3 5
1 3 5

它只会终止程序并且没关系,但输入如下:

Put in numbers
1
3
1 3
1 3
Citizens number does not match, try again
1
3
1 3 5

程序将在我无法进入第四行之前终止。

作为我放入的测试

for(int n=0;n<length;n+=1)
{
    System.out.println(dspeed[n]);
}

在Arrays.sort(dspeed [n])和break之间,结果是这样的

Put in numbers
1
3
1 3
1 3
Citizens number do not match, try again
1
3
1 3 5
3

没有任何意义,因为它给了我3的输出而不让我进入第二行。所以它就像是跳过代码的一部分。为什么会发生这种情况?如何解决这个问题?

编辑:对于mission1,它假设得到一个数字(比方说x),然后从下一行得到x个不同的数字。然后,它应该将这些数字放在一个数组中并对它们进行排序

1 个答案:

答案 0 :(得分:0)

在写我的答案时,我意识到你的错误的实际原因是什么;看看这段代码:

lines=miss1.nextLine();
lines=lines.replaceAll("\\s", "");
length=lines.length();
lines2=miss1.nextLine();
/*Should be lines2!*/
lines2=lines.replaceAll("\\s", "");
/*Should be lines2!*/
length2=lines.length();

您正在使用lines表示所有变量。将lineslines2变量的lines2 = ...更改为length2可修复您发布的问题。我也换了

if (citizens != length || citizens != length2) {
    /*If you decide to keep this, it should not be a "IndexOutOfBoundsException 
    (since no index was out of bounds) but should perhaps be a "IllegalArgumentException" 
    since you supplied it illegal arguments.*/
    throw new IndexOutOfBoundsException("Numebr of citizens do not match. Please enter numbers again");
} else if (citizens == length && citizens == length2) { ... }

with

if (citizens == length && citizens == length2) { ... }

否则,如果连续两次输入无效输入,则会导致程序崩溃。

这部分答案应该被视为代码审查,并在此(尝试)帮助改进您的代码结构。实际答案如上。

这是整个班级代码。这似乎已经解决了你的错误,我还重新构建了一些代码并添加了注释,解释了我编辑的内容以及我重构它的原因。如果有任何不清楚或者实际上没有解决您的错误,请发表评论。请注意,为清楚起见,这些答案中没有输入。

public class Cycle {
/* Important! We can re-use the same scanner for all inputs. */
final Scanner in;

public Cycle() {
    in = new Scanner(System.in);
    run();
    in.close();
}

private void run() {
    System.out.println("Input mission number.");
    final int indicator = Integer.parseInt(in.nextLine());
    if (indicator == 1) {
        mission1();
    } else if (indicator == 2) {
        // etc
    }
}

private void mission1() {
    while (true) {
        System.out.println("Input number of citizens.");
        final int citizens = Integer.valueOf(in.nextLine());

        /* We don't edit these two first inputs just yet since we
         * have to use the un-edited inputs later in our if-else
         * statement. */
        System.out.println("Input first number(s).");
        final String inputOne = in.nextLine();
        System.out.println("Input second number(s).");
        final String inputTwo = in.nextLine();

        final String lines = inputOne.replaceAll("\\s", "");
        final int length = lines.length();
        final String lines2 = inputTwo.replaceAll("\\s", "");
        final int length2 = lines2.length();

        if (citizens != length || citizens != length2) {
            /* If the number of citizens doesn't match it just
             * continues the while-loop and does it all over
             * again. */
            System.out.println("Citizens number do not match, try again");
        } else {
            /* Here we use the unedited inputs from before - which
             * is why we didn't edit them. */
            final int[] dspeed = createArrayFromInput(inputOne);
            final int[] pspeed = createArrayFromInput(inputTwo);
            break;
        }
    }
}

/* We shouldn't have duplicated code - use a method instead. */
private int[] createArrayFromInput(final String input) {
    final String[] strs = input.trim().split("\\s+");
    /* The arrays should be the same size so use 'strs.length' as
     * length */
    final int[] speed = new int[strs.length];
    for (int i = 0; i < strs.length; i++) {
        speed[i] = Integer.parseInt(strs[i]);
    }
    Arrays.sort(speed);
    return speed;
}

public static void main(final String[] arg) {
    /*We should use a class instance instead of static methods and variables.*/
    new Cycle();
}
}
相关问题