微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么将值设置为数组的特定下标不能打印出来?

如何解决为什么将值设置为数组的特定下标不能打印出来?

在下面的代码中,由于某些原因,尽管我分配了encodingCharacter = encodeArray [j];的值,但由于某种原因。之后,我尝试将特定的下标值打印出来。但是由于某种原因,该声明引起了问题。运行该程序后,我看到它运行了2空行,我不知道为什么不能将char值'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()something of the sort

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。