![](/img/trans.png)
[英]why does my program print the numbers in my array as zeros but still print the largest value out of the array?
[英]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.