如何返回一个未知值的字符数组?

huangapple go评论67阅读模式
英文:

How do I return a char array of an unknown value?

问题

import java.util.Scanner;

public class UserInputDemo1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Would you like to encode or decode a message.");
        System.out.println("If you would like to encode a message enter true. If you would like to decode a message enter false");
        System.out.println("Note that blank spaces are separated by a space and the message terminates with a period.");

        boolean encodeOrDecode = input.nextBoolean();

        if (encodeOrDecode) {
            Scanner scan = new Scanner(System.in);

            System.out.println("Please enter the message you would like to be encoded.");

            String encodeMessage = scan.nextLine();

            char[] encodeCharArray = encodeMessage.toCharArray();

            encodeMessage(encodeCharArray);
        }

        if (!encodeOrDecode) {
            Scanner scan = new Scanner(System.in);

            System.out.println("Please enter the message you would like to be decoded.");

            String decodeMessage = scan.nextLine();

            char[] decodeCharArray = decodeMessage.toCharArray();
        }
    }

    public static void encodeMessage(char encoding[]) {
        for (int j = 0; j < encoding.length; j++) {
            char encodingCharacter = encoding[j];

            if (encodingCharacter == 'a') {
                char encodingCharactera = 'a' + 25;
                System.out.println(encodingCharactera);
            }

            if (encodingCharacter == 'b') {
                char encodingCharacterb = 'b' + 23;
                System.out.println(encodingCharacterb);
            }
        }
    }
}
英文:

I have a program that is supposed to execute a simple cipher where it prompts the user whether they would like to encode or decode a message. The cipher is simple (a = z, b = y, c = x.) As you see below I can convert the individual char. But I haven't the slightest clue on how to make the converted characters into an array since there are an unknown number of characters in the string and whether it would be better to convert it into a string in the method or back in main.

import java.util.Scanner;  
public class UserInputDemo1  
{  
public static void main(String[] args)  
{  
Scanner input = new Scanner(System.in);
//creating scanner named input.
System.out.println (&quot;Would you like to encode or decode a message.&quot;); 
System.out.println (&quot;If you would like to encode a message enter true. If you would like to decode a message enter false&quot;);
System.out.println(&quot;Note that blank spaces are seperated by a space and the message terminates with a period.&quot;);
boolean encodeOrDecode = input.nextBoolean();
if (encodeOrDecode)
{
Scanner scan = new Scanner(System.in);   
System.out.println(&quot;Please enter the message you would like to be encoded.&quot;);  
String encodeMessage = scan.nextLine();  
char[] encodeCharArray = encodeMessage.toCharArray();
encodeMessage(encodeCharArray);
}
if (!encodeOrDecode)
{
Scanner scan = new Scanner(System.in); 
System.out.println(&quot;Please enter the message you would like to be decoded.&quot;);  
String decodeMessage = scan.nextLine();                
char[] decodeCharArray = decodeMessage.toCharArray();
}
}
public static void encodeMessage (char encoding [])
{
for (int j = 0; j &lt; encoding.length; j++ )
{
char encodingCharacter = encoding[j];
if (encodingCharacter == &#39;a&#39;)
{
char encodingCharactera = &#39;a&#39; + 25;
System.out.println(encodingCharactera);
}
if (encodingCharacter == &#39;b&#39;)
{
char encodingCharacterb = &#39;b&#39; + 23;
System.out.println(encodingCharacterb);
}
}
}
} 

答案1

得分: 0

以下是您要的翻译内容:

只是为了好玩。这是我使用 Stream 的实现。

public static String encodeMessage(String encoding) {
    return Arrays.stream(encoding.toCharArray())
        .map(c -> 219 - (int)(c))
        .map(c -> new String(new char[] {(char)c}))
        .collect(Collectors.joining());
}
英文:

Just for fun. This is my implementation with Stream.

public static String encodeMessage (String encoding) {
return Arrays.stream(encoding.toCharArray())
.map(c -&gt; 219-(int)(c))
.map(c -&gt; new String((char)c))
.collect(Collectors.joining())
}

huangapple
  • 本文由 发表于 2020年9月9日 07:33:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63802849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定