简体   繁体   English

如何将数组元素作为输入

[英]How to take array elements as input

My program is to accept words (given as number of test cases) and print them out in reversed order. 我的程序是接受单词(以测试用例的数量给出)并以相反的顺序打印出来。 The problem is that whatever input of array size I may give, it only accepts just one word (and rest as blank). 问题是,无论我提供多少数组大小的输入,它都只接受一个单词(其余为空白)。 Can anyone help me figure out why? 谁能帮我找出原因? Here's the code: 这是代码:

import java.util.*;
public class terrible {
    public static void main(String args[]){
        Scanner input  = new Scanner(System.in);
        int test = input.nextInt();
        while(test>0){
            String str = input.nextLine();char c[] = str.toCharArray();
            for(int i=0;i<str.length();i++){
                System.out.print(c[str.length()-i-1]);
            }
            System.out.println();
            test--;
        }

    }
}

Certain versions of Java don't let you take an int and then a string as input from the same Scanner . Java的某些版本不允许您将intstring作为来自同一Scanner输入。 You can create another Scanner , like 您可以创建另一个Scanner ,例如

Scanner input2 = new Scanner(System.in);

and then do 然后做

String str = input2.nextLine();

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

相关问题 如何在 java 中不定义大小的情况下获取 N 个元素的输入数组 - How to take input array of N elements without defining size in java java - 如何将两个不同的字符串数组元素作为一个二维数组的输入 - How to take two different string array elements as input to one 2D array in java 输入一个数组的元素个数,将数组输入并显示数组的奇偶元素在其指定的arrays - Take input of number of elements of an array, and input the array and display the odd and even elements of the array in their specified arrays 如何使用 java 在数组中获取用户输入? - how to take user input in Array using java? 如何在Java中获取“ char”数组的输入? - How to take input for 'char' array in Java? 如何获取输入并保存到数组中,然后打印数组输入? - How to take input and save into an array and then print the array input? 取数组中元素的平均值 - Take the average of elements in array 如何扫描数组并将输入与数组元素进行比较 - How to scan an array and compare an input to the elements of the array 如何获取Json数组中的元素并将内容放入arraylist中? - How can I take the elements in a Json array and put the contents in an arraylist? 如何从Java中的文本文件获取数组大小和元素的输入? - How to take array size and elements' inputs from a text file in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM