为什么将一个值设置到数组的特定下标上后不会将其打印出来?

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

Why does setting a value to a specific subscript of an array not print it out?

问题

在下面的代码中在底部由于某种原因尽管我赋予了```encodingCharacter = encodeArray[j];```的值之后我尝试打印出该特定下标的值但由于某种原因那个语句引起了问题在运行程序后我看到它打印了两行空白我不知道为什么我不能将字符值```'z'```赋予```encodingCharacter```。


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[]) {
        char encodeArray[] = new char[encoding.length];
        
        for (int j = 0; j < encoding.length; j++) {
            char encodingCharacter = encoding[j];
            
            if (encodingCharacter == 'a') {
                encodingCharacter = 'z';

                System.out.println(encodingCharacter);
                
                encodingCharacter = encodeArray[j];
                
                System.out.println(encodeArray[j]);
            }
            
            if (encodingCharacter == 'b') {
                encodingCharacter = 'y';

                System.out.println(encodingCharacter);
                
                encodingCharacter = encodeArray[j];
            }
        }
        
        String encodedArray = new String(encodeArray);
        
        System.out.println(encodeArray);
    }
}
英文:

In the code below, at the bottom, for some reason although I assign the value of encodingCharacter = encodeArray[j];. After that I try to print that specific subscript value out. But for some reason that statement is causing issues. After I run the program I see that it ran 2 empty lines, I don't know why I can't assign the char value 'z' to encodingCharacter.

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 [])
{
char encodeArray [] = new char [encoding.length];
for (int j = 0; j &lt; encoding.length; j++ )
{
char encodingCharacter = encoding[j];
if (encodingCharacter == &#39;a&#39;)
{
encodingCharacter = &#39;z&#39;;
System.out.println(encodingCharacter);
encodingCharacter = encodeArray[j];
System.out.println(encodeArray[j]);
}
if (encodingCharacter == &#39;b&#39;)
{
encodingCharacter = &#39;y&#39;;
System.out.println(encodingCharacter);
encodingCharacter = encodeArray[j];
}
}
String encodedArray = new String(encodeArray);
System.out.println (encodeArray);
}
}

答案1

得分: 0

在第 15 行发生了 java.util.InputMismatchException 异常。

您的 input.nextBoolean() 会返回输入中的下一个标记是否为布尔值,而不是是否有另一个字符。

我相信您在寻找 input.hasNext(),或者类似的内容。

<!-- 这是我的第一个答案!- 2021/4/30 -->

英文:

At line 15 is where the java.util.InputMismatchException occurs.

Your input.nextBoolean() returns whether the next token of the input is a boolean, rather than whether there is another character.

I believe you are looking for input.hasNext(), or something of the sort.

<!-- This is my first answer! - 2021/4/30 -->

huangapple
  • 本文由 发表于 2020年9月10日 08:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63821184.html
匿名

发表评论

匿名网友

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

确定