簡體   English   中英

為什么將值設置為數組的特定下標不會將其打印出來?

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

在下面的代碼中,在底部,出於某種原因,雖然我分配了 encodingCharacter = encodeArray[j]; 的值。 之后,我嘗試打印出該特定下標值。 但出於某種原因,該聲明引起了問題。 在我運行程序后,我看到它運行了 2 個空行,我不知道為什么我不能將字符值 'z' 分配給 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 ("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 seperated 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);
    }
}

15行是 java.util.InputMismatchException 發生的地方。

您的input.nextBoolean()返回輸入的下一個標記是否是布爾值,而不是是否有另一個字符。

我相信您正在尋找input.hasNext()類似的東西

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM