gpt4 book ai didi

java - 尝试将消息中的字母转换为数字时没有得到任何输出

转载 作者:太空宇宙 更新时间:2023-11-04 07:22:41 27 4
gpt4 key购买 nike

我正在尝试制作一个程序,用户输入一条消息,程序将其转换为数字(例如a=1、b=2等)。但是,我没有得到任何输出!任何帮助,将不胜感激。我的代码如下。

import java.io.PrintStream;
import java.util.*;


public class AllRand25 {
public static void main(String[] args) {


int[] letterArray;
int[] wordArray;
int l = 0;
String messageWord;
char messageChar;

letterArray = new int[26];
int[] messageArray = new int[10];

int y = 0;

System.out.print("Enter your message");
Scanner scanner = new Scanner(System.in);
String message = scanner.nextLine();

while (scanner.hasNext()) {

messageWord = scanner.next(message);

l = messageWord.length();
Scanner charScan = new Scanner(messageWord);

for(int i = 0; i < messageWord.length(); i++){

messageChar = messageWord.charAt(i);


if (messageChar == 'a' || messageChar == 'A' || messageChar == 'A') {
messageArray[y] = 1;

} else if (messageChar == 'b' || messageChar == 'B') {
messageArray[y] = 2;
} else if (messageChar == 'c' || messageChar == 'C') {
messageArray[y] = 3;
} else if (messageChar == 'd' || messageChar == 'D') {
messageArray[y] = 4;
} else if (messageChar == 'e' || messageChar == 'E') {
messageArray[y] = 5;
} else if (messageChar == 'f' || messageChar == 'F') {
messageArray[y] = 6;
} else if (messageChar == 'g' || messageChar == 'G') {
messageArray[y] = 7;
} else if (messageChar == 'h' || messageChar == 'H') {
messageArray[y] = 8;
} else if (messageChar == 'i' || messageChar == 'I') {
messageArray[y] = 9;
} else if (messageChar == 'j' || messageChar == 'J') {
messageArray[y] = 10;
} else if (messageChar == 'k' || messageChar == 'K') {
messageArray[y] = 11;
} else if (messageChar == 'l' || messageChar == 'L') {
messageArray[y] = 12;
} else if (messageChar == 'm' || messageChar == 'M') {
messageArray[y] = 13;
} else if (messageChar == 'n' || messageChar == 'N') {
messageArray[y] = 14;
} else if (messageChar == 'o' || messageChar == 'O') {
messageArray[y] = 15;
} else if (messageChar == 'p' || messageChar == 'P') {
messageArray[y] = 16;
} else if (messageChar == 'q' || messageChar == 'Q') {
messageArray[y] = 17;
} else if (messageChar == 'r' || messageChar == 'R') {
messageArray[y] = 18;
} else if (messageChar == 's' || messageChar == 'S') {
messageArray[y] = 19;
} else if (messageChar == 't' || messageChar == 'T') {
messageArray[y] = 20;
} else if (messageChar == 'u' || messageChar == 'U') {
messageArray[y] = 21;
} else if (messageChar == 'v' || messageChar == 'V') {
messageArray[y] = 22;
} else if (messageChar == 'w' || messageChar == 'W') {
messageArray[y] = 23;
} else if (messageChar == 'x' || messageChar == 'X') {
messageArray[y] = 24;
} else if (messageChar == 'y' || messageChar == 'Y') {
messageArray[y] = 25;
} else if (messageChar == 'z' || messageChar == 'Z') {
messageArray[y] = 26;
}
else if (messageChar == ' ') {
messageArray[y] = 27;
}

y++;
}

}
for(int i = 0; i < messageArray.length; i++){
System.out.println(messageArray[i]);
}





}
}

最佳答案

你有很多错误。太多了,无法指出,如果我用注释显示正确的代码,那就更简单了:

public static void main(String[] args) throws Exception {

int[] wordArray;
String messageWord;
char messageChar;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your message: ");
messageWord = scanner.nextLine(); // read once before entering loop

while (!messageWord.equals("exit")) { // notice the correct condition

// these two variables should be reset for each word
int y = 0;
// and each messageArray should be of the right size
int[] messageArray = new int[messageWord.length()];

for (int i = 0; i < messageWord.length(); i++) {
messageChar = messageWord.charAt(i);
// *** insert all those if conditions here ***
y++;
}

// this goes inside the main loop
for (int j = 0; j < messageArray.length; j++)
System.out.print(messageArray[j] + " ");

// we have to ask for the next word inside the main loop
System.out.print("\nEnter your message: ");
messageWord = scanner.nextLine();

}

}

请注意,要退出循环,最好有一个可以用作条件的特殊字符串 - 我为此使用单词“exit”。

关于java - 尝试将消息中的字母转换为数字时没有得到任何输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072443/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com