难以从Java控制台中正确读取输入

难以从Java控制台中正确读取输入

本文介绍了难以从Java控制台中正确读取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从控制台读取输入时遇到问题,如下所述:

I am having problem to read inputs from console as stated below:

输入:
输入的第一行包括测试用例的数量. T测试用例的说明如下:
每个测试用例的第一行包含数组的大小,第二行包含数组的元素,第三行包含差k.

Input:
The first line of input consists number of the test cases. The description of T test cases is as follows:
The first line of each test case contains the size of the array, the second line has the elements of the array and the third line consists of the difference k.

Example:
Input:
2
5
1 5 4 1 2
0
3
1 5 3
2

这是我的代码及其不正确的输出:

Here is my code and its incorrect output:

    Scanner input = new Scanner(System.in);
    int T;
    T = input.nextInt();
    int[] k_holder = new int[T];
    int[] n_holder = new int[T];
    int[][] array  = new int[T][10000];

    for (int i=0; i<T; i++) { // length of test
        n_holder[i] = input.nextInt();
        String tmp[] = input.next().split(" ");

        for (int j=0;j<tmp.length; j++) {
            array[i][j] = Integer.parseInt(tmp[j]);
        }

        int K = input.nextInt(); // difference
        k_holder[i] = K;
    }
            System.out.println("===============");

    for (int i=0; i<T; i++) {
        System.out.println(n_holder[i]);

        for (int j=0; j<n_holder[i]; j++) {
            System.out.print(array[i][j] + " ");
        }
        System.out.print("\n" + k_holder[i]);
    }


Output:
2
5
1 5 4 1 2
===============
5
1 0 0 0 0
54
1 0 0 0
2

我在array_line之后键入回车(在上面的示例中array_line是1 5 4 1 2),我得到了奇怪的输出

As soon as I type enter after array_line (in above case array_line is 1 5 4 1 2), I got strange output

推荐答案

由于您没有提供预期的结果.很难说出您将要做什么,以下是我的建议.

As you didn't provide your expected result. It is hard to tell what exactly you are going to do, the following are my suggestions.

    在所有nextInt()语句之后
  1. 添加input.nextLine();nextInt()无法使用换行符(\ n),因此需要input.nextLine()捕获它,否则结果将被弄乱.

  1. add input.nextLine(); after all your nextInt() statements, nextInt() cannot consume the newline character(\n), therefore it needs input.nextLine() capture it, or the result will messed up.

使用String tmp[] = input.nextLine().split(" ");解析以空格分隔的行,next()仅处理第一个令牌.

use String tmp[] = input.nextLine().split(" "); to parse your space separated line, next() only handles the first token.

import java.io.BufferedWriter; 导入java.io.FileWriter; 导入java.io.IOException; 导入java.time.LocalDateTime; 导入java.util.Scanner;

import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.time.LocalDateTime; import java.util.Scanner;

public class Main{
    public static void main(String args[]) throws IOException{
        new Main().go();
    }

    public void go() throws IOException{
        Scanner input = new Scanner(System.in);
        int T;
        T = input.nextInt();
        input.nextLine();
        System.out.println("T is " + T);
        int[] k_holder = new int[T];
        int[] n_holder = new int[T];
        int[][] array  = new int[T][10000];

        for (int i=0; i<T; i++) { // length of array
            n_holder[i] = input.nextInt();
            input.nextLine();
            System.out.println("n_holder[" +i + "] is " + n_holder[i]);

            String tmp[] = input.nextLine().split(" ");
           for(String e : tmp){
                System.out.print(" " + e);
            }

            for (int j=0;j<tmp.length; j++) {
                array[i][j] = Integer.parseInt(tmp[j]);
            }

            int K = input.nextInt(); // difference
            input.nextLine();
            k_holder[i] = K;
        }
        System.out.println("===============");

        for (int i=0; i<T; i++) {
            System.out.println(n_holder[i]);

            for (int j=0; j<n_holder[i]; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.print("\n" + k_holder[i]);
        }
    }
}

我的测试:

2
T is 2
5
n_holder[0] is 5
1 5 4 1 3
 1 5 4 1 3
0
3
n_holder[1] is 3
1 5 3
 1 5 3
2
===============
5
1 5 4 1 3
03
1 5 3
2

这篇关于难以从Java控制台中正确读取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:40