定义并初始化一个数组来存储一定数量的字符,并将它们显示为输出

时间:2022-07-23 19:35:11

so my main target is this: Write an line of code that enables me to save certain amount of chars (5 in this example is what I tried) into it and how to display them as output, here is where I am currently:

因此,我的主要目标是:编写一行代码,使我能够在其中保存一定数量的字符(本例中为5),以及如何将它们显示为输出,这就是我目前的情况:

public class TryingCharArrays
{
    public static void main(String[]args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Write 5 characters / symbols: ");
        char[] simbolet = new char[5];
        simbolet = sc.nextChar();    // obvsiously this does not work 'sc.nextChar(); ' however I have no idea on how to write it correctly
        simbolet.toString();

        System.out.println(Arrays.toString(simbolet));
    }
}

I know that the first line of defining the char array is correct (at least for my knowledge):

我知道定义char数组的第一行是正确的(至少就我所知):

char[] simbolet2 = new char[5];

However I have no idea how to get the user-input and store inside the array, also I can't do the nextChar(); ti display them as output.

但是我不知道如何在数组中获取用户输入和存储,也不能做nextChar();ti将它们显示为输出。

So would love some help / hints. Thanks in advance

我也想要一些帮助/暗示。谢谢提前

2 个解决方案

#1


0  

You can read the whole input as a String and convert it toCharArray()

可以将整个输入读入为字符串并将其转换为chararray ()

In your case I would read the input as a String and then convert it to an array. In this case you do not need any loops.

在您的例子中,我将把输入作为字符串读取,然后将其转换为数组。在这种情况下,不需要任何循环。

And by the way the line simbolet.toString(); is of no use because the toString() method returns a new String and does not convert the original one stored in simbolet to a String.

顺便说一句,直线simbo。tostring ();因为toString()方法返回一个新的字符串,并且不将存储在simbolet中的原始字符串转换为字符串,所以没有任何用处。

Update based on the comments

根据评论进行更新

If you want to achieve to fill in the 5 characters of the array I do not understand why you ask for 3 characters in your output... but never mind.

如果你想要填充数组的5个字符,我不明白你为什么要在输出中输入3个字符……但没关系。

You can do this in two ways. Once is to read the whole string as once and convert it to a character array:

你可以用两种方法。一次是读取整个字符串一次,并将其转换为字符数组:

Scanner sc = new Scanner(System.in);
System.out.println("Shkruani 3 karaktere / simbole: "); 
char[] simbolet = sc.nextLine().toCharArray();

In this case the length of the simbolet array depends on how long the input is. It can be shorter, equal or longer than 5 characters. You could add verification and repeat the request to the user if he/she provides less input.

在这种情况下,simbolet数组的长度取决于输入的长度。它可以比5个字符短、相等或长。您可以添加验证并向用户重复请求,如果他/她提供的输入较少。

Scanner sc = new Scanner(System.in);
System.out.println("Shkruani 3 karaktere / simbole: "); 
char[] simbolet = sc.nextLine().substring(0,5).toCharArray();

In this case if the input is shorter than 5 you get an exception of course.

在这种情况下,如果输入小于5,当然会出现异常。

If you want to read one line and get only the first five symbols you can have a substring:

如果你想读一行,只得到前五个符号,你可以有一个子字符串:

If you want to get 5 characters from the user then use the loop. But declare the simbolet array outside of the loop or you will lose previously entered input:

如果您想从用户那里获得5个字符,那么使用循环。但在循环外部声明simbolet数组,否则将丢失先前输入的输入:

int end = 5; 
Scanner sc = new Scanner(System.in);
char[] simbolet = new char[end]; 
for(int i = 0; i < end; i++) { 
    System.out.println("Enter 1 character or symbol: "); 
    simbolet[i] = sc.nextLine().charAt(0); 
    System.out.println(simbolet);
}
sc.close();

Or you can combine the two approaches together:

或者你可以把这两种方法结合起来:

    int end = 5;
    Scanner sc = new Scanner(System.in);
    char[] simbolet = new char[end];
    System.out.println("Enter the symbols: ");
    String input = sc.nextLine();
    for (int i = 0; i < end && i < input.length(); i++) {
        simbolet[i] = input.charAt(i);
    }
    System.out.println(simbolet);
    sc.close();

#2


0  

You could take the first character from Scanner.next:

你可以从扫描程序中提取第一个字符。

char c = reader.next().charAt(0);

To consume exactly one character you could use:

要正确使用一个字符,你可以:

char c = reader.findInLine(".").charAt(0);

To consume strictly one character you could use:

要严格使用一个角色,你可以使用:

char c = reader.next(".").charAt(0);

So You can convert your code like this:-

所以你可以这样转换你的代码:-

Scanner sc = new Scanner(System.in);
        System.out.println("Write 5 characters / symbols: ");
        char[] simbolet = new char[5];
        simbolet[0] = (char)sc.next().charAt(0);
        simbolet[1] = (char)sc.next().charAt(0);
        simbolet[2] = (char)sc.next().charAt(0);
        simbolet[3] = (char)sc.next().charAt(0);
        simbolet[4] = (char)sc.next().charAt(0);
        simbolet.toString();
        System.out.println(Arrays.toString(simbolet));

#1


0  

You can read the whole input as a String and convert it toCharArray()

可以将整个输入读入为字符串并将其转换为chararray ()

In your case I would read the input as a String and then convert it to an array. In this case you do not need any loops.

在您的例子中,我将把输入作为字符串读取,然后将其转换为数组。在这种情况下,不需要任何循环。

And by the way the line simbolet.toString(); is of no use because the toString() method returns a new String and does not convert the original one stored in simbolet to a String.

顺便说一句,直线simbo。tostring ();因为toString()方法返回一个新的字符串,并且不将存储在simbolet中的原始字符串转换为字符串,所以没有任何用处。

Update based on the comments

根据评论进行更新

If you want to achieve to fill in the 5 characters of the array I do not understand why you ask for 3 characters in your output... but never mind.

如果你想要填充数组的5个字符,我不明白你为什么要在输出中输入3个字符……但没关系。

You can do this in two ways. Once is to read the whole string as once and convert it to a character array:

你可以用两种方法。一次是读取整个字符串一次,并将其转换为字符数组:

Scanner sc = new Scanner(System.in);
System.out.println("Shkruani 3 karaktere / simbole: "); 
char[] simbolet = sc.nextLine().toCharArray();

In this case the length of the simbolet array depends on how long the input is. It can be shorter, equal or longer than 5 characters. You could add verification and repeat the request to the user if he/she provides less input.

在这种情况下,simbolet数组的长度取决于输入的长度。它可以比5个字符短、相等或长。您可以添加验证并向用户重复请求,如果他/她提供的输入较少。

Scanner sc = new Scanner(System.in);
System.out.println("Shkruani 3 karaktere / simbole: "); 
char[] simbolet = sc.nextLine().substring(0,5).toCharArray();

In this case if the input is shorter than 5 you get an exception of course.

在这种情况下,如果输入小于5,当然会出现异常。

If you want to read one line and get only the first five symbols you can have a substring:

如果你想读一行,只得到前五个符号,你可以有一个子字符串:

If you want to get 5 characters from the user then use the loop. But declare the simbolet array outside of the loop or you will lose previously entered input:

如果您想从用户那里获得5个字符,那么使用循环。但在循环外部声明simbolet数组,否则将丢失先前输入的输入:

int end = 5; 
Scanner sc = new Scanner(System.in);
char[] simbolet = new char[end]; 
for(int i = 0; i < end; i++) { 
    System.out.println("Enter 1 character or symbol: "); 
    simbolet[i] = sc.nextLine().charAt(0); 
    System.out.println(simbolet);
}
sc.close();

Or you can combine the two approaches together:

或者你可以把这两种方法结合起来:

    int end = 5;
    Scanner sc = new Scanner(System.in);
    char[] simbolet = new char[end];
    System.out.println("Enter the symbols: ");
    String input = sc.nextLine();
    for (int i = 0; i < end && i < input.length(); i++) {
        simbolet[i] = input.charAt(i);
    }
    System.out.println(simbolet);
    sc.close();

#2


0  

You could take the first character from Scanner.next:

你可以从扫描程序中提取第一个字符。

char c = reader.next().charAt(0);

To consume exactly one character you could use:

要正确使用一个字符,你可以:

char c = reader.findInLine(".").charAt(0);

To consume strictly one character you could use:

要严格使用一个角色,你可以使用:

char c = reader.next(".").charAt(0);

So You can convert your code like this:-

所以你可以这样转换你的代码:-

Scanner sc = new Scanner(System.in);
        System.out.println("Write 5 characters / symbols: ");
        char[] simbolet = new char[5];
        simbolet[0] = (char)sc.next().charAt(0);
        simbolet[1] = (char)sc.next().charAt(0);
        simbolet[2] = (char)sc.next().charAt(0);
        simbolet[3] = (char)sc.next().charAt(0);
        simbolet[4] = (char)sc.next().charAt(0);
        simbolet.toString();
        System.out.println(Arrays.toString(simbolet));