我正在创建一个程序,它采用 32 - 126 的 ASCII 代码之间的单个字符并将结果作为二进制返回。这是一堆带有 .contains("")的if语句。该代码在返回单个字符时有效,但在输入单词时,字符串以二进制重新字母顺序返回。因此,如果我输入 Lauren 的名称,它会像这样以二进制形式出现:010011000110000101100101011011100111001001110101-Laenru(大写字母仍然是第一位的)。
这是我的所有代码:
主.java
import java.util.Scanner;
public class Main {
public static Scanner input = new Scanner(System.in);
public static void main (String[] args) {
Letters l = new Letters();
l.Letters();
}
}
字母.java
公共类字母扩展主要{
@SuppressWarnings("null")
public static void Letters () {
System.out.println("Enter your letter(s)");
String givenLetters = input.next();
if (givenLetters.contains("A"))
System.out.println("01000001");
if (givenLetters.contains("B"))
System.out.println("01000010");
if (givenLetters.contains("C"))
System.out.println("01000011");
if (givenLetters.contains("D"))
System.out.println("01000100");
if (givenLetters.contains("E"))
System.out.println("01000101");
if (givenLetters.contains("F"))
System.out.println("01000110");
if (givenLetters.contains("G"))
System.out.println("01000111");
if (givenLetters.contains("H"))
System.out.println("01001000");
if (givenLetters.contains("I"))
System.out.println("01001001");
if (givenLetters.contains("J"))
System.out.println("01001010");
if (givenLetters.contains("K"))
System.out.println("01001011");
if (givenLetters.contains("L"))
System.out.println("01001100");
if (givenLetters.contains("M"))
System.out.println("01001101");
if (givenLetters.contains("N"))
System.out.println("01001110");
if (givenLetters.contains("O"))
System.out.println("01001111");
if (givenLetters.contains("P"))
System.out.println("01010000");
if (givenLetters.contains("Q"))
System.out.println("01010001");
if (givenLetters.contains("R"))
System.out.println("01010010");
if (givenLetters.contains("S"))
System.out.println("01010011");
if (givenLetters.contains("T"))
System.out.println("01010100");
if (givenLetters.contains("U"))
System.out.println("01010101");
if (givenLetters.contains("V"))
System.out.println("01010110");
if (givenLetters.contains("W"))
System.out.println("01010111");
if (givenLetters.contains("X"))
System.out.println("01011000");
if (givenLetters.contains("Y"))
System.out.println("01011001");
if (givenLetters.contains("Z"))
System.out.println("01011010");
if (givenLetters.contains(" "))
System.out.println("00100000");
if (givenLetters.contains("a"))
System.out.println("01100001");
if (givenLetters.contains("b"))
System.out.println("01100010");
if (givenLetters.contains("c"))
System.out.println("01100011");
if (givenLetters.contains("d"))
System.out.println("01100100");
if (givenLetters.contains("e"))
System.out.println("01100101");
if (givenLetters.contains("f"))
System.out.println("01100110");
if (givenLetters.contains("g"))
System.out.println("01100111");
if (givenLetters.contains("h"))
System.out.println("01101000");
if (givenLetters.contains("i"))
System.out.println("01101001");
if (givenLetters.contains("j"))
System.out.println("01101010");
if (givenLetters.contains("k"))
System.out.println("01101011");
if (givenLetters.contains("l"))
System.out.println("01101100");
if (givenLetters.contains("m"))
System.out.println("01101101");
if (givenLetters.contains("n"))
System.out.println("01101110");
if (givenLetters.contains("o"))
System.out.println("01101111");
if (givenLetters.contains("p"))
System.out.println("01110000");
if (givenLetters.contains("q"))
System.out.println("01110001");
if (givenLetters.contains("r"))
System.out.println("01110010");
if (givenLetters.contains("s"))
System.out.println("01110011");
if (givenLetters.contains("t"))
System.out.println("01110100");
if (givenLetters.contains("u"))
System.out.println("01110101");
if (givenLetters.contains("v"))
System.out.println("01110110");
if (givenLetters.contains("w"))
System.out.println("01110111");
if (givenLetters.contains("x"))
System.out.println("01111000");
if (givenLetters.contains("y"))
System.out.println("01111001");
if (givenLetters.contains("z"))
System.out.println("01111010");
}
}
如果这意味着我必须添加代码以重新排列字母或者我需要修复错误代码中的某些内容,我希望字符串以用户输入的方式出现。先谢谢了!:)