字符串中重复的字符不发送

huangapple go评论65阅读模式
英文:

Repeating characters in string do not send

问题

以下是代码的翻译部分:

import java.util.Scanner;

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

        Scanner scanner = new Scanner(System.in);
 
        char A = 'Z';
        char B = 'Y';
        char C = 'X';
        char D = 'W';
        char E = 'V';
        char F = 'U';
        char G = 'T';
        char H = 'S';
        char I = 'R';
        char J = 'Q';
        char K = 'P';
        char L = 'O';
        char M = 'N';
        char N = 'M';
        char O = 'L';
        char P = 'K';
        char Q = 'J';
        char R = 'I';
        char S = 'H';
        char T = 'G';
        char U = 'F';
        char V = 'E';
        char W = 'D';
        char X = 'C';
        char Y = 'B';
        char Z = 'A';
 
        System.out.println("请输入您想要翻转的消息:");
        String message = scanner.nextLine().toUpperCase();


        if (new String(message).contains("A")) {
            System.out.print(A);
        }
        if (new String(message).contains("B")) {
            System.out.print(B);
        }
        if (new String(message).contains("C")) {
            System.out.print(C);
        }
        if (new String(message).contains("D")) {
            System.out.print(D);
        }
        if (new String(message).contains("E")) {
            System.out.print(E);
        }
        if (new String(message).contains("F")) {
            System.out.print(F);
        }
        if (new String(message).contains("G")) {
            System.out.print(G);
        }
        if (new String(message).contains("H")) {
            System.out.print(H);
        }
        if (new String(message).contains("I")) {
            System.out.print(I);
        }
        if (new String(message).contains("J")) {
            System.out.print(J);
        }
        if (new String(message).contains("K")) {
            System.out.print(K);
        }
        if (new String(message).contains("L")) {
            System.out.print(L);
        }
        if (new String(message).contains("M")) {
            System.out.print(M);
        }
        if (new String(message).contains("N")) {
            System.out.print(N);
        }
        if (new String(message).contains("O")) {
            System.print(O);
        }
        if (new String(message).contains("P")) {
            System.out.print(P);
        }
        if (new String(message).contains("Q")) {
            System.out.print(Q);
        }
        if (new String(message).contains("R")) {
            System.out.print(R);
        }
        if (new String(message).contains("S")) {
            System.out.print(S);
        }
        if (new String(message).contains("T")) {
            System.out.print(T);
        }
        if (new String(message).contains("U")) {
            System.out.print(U);
        }
        if (new String(message).contains("V")) {
            System.out.print(V);
        }
        if (new String(message).contains("W")) {
            System.out.print(W);
        }
        if (new String(message).contains("X")) {
            System.out.print(X);
        }
        if (new String(message).contains("Y")) {
            System.out.print(Y);
        }
        if (new String(message).contains("Z")) {
            System.out.print(Z);
        }

 
    }
}
英文:

Hi so I know there is most likely a way better way to write this that is more efficient but I am just playing around with Java and am making a program where if you type a work it is the same work but the letter opposite in the alphabet. The problem I am having is that repeating letters do not repeat in the execution of the program. Is there a way to fix it? Here is the program

import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char A = 'Z';
char B = 'Y';
char C = 'X';
char D = 'W';
char E = 'V';
char F = 'U';
char G = 'T';
char H = 'S';
char I = 'R';
char J = 'Q';
char K = 'P';
char L = 'O';
char M = 'N';
char N = 'M';
char O = 'L';
char P = 'K';
char Q = 'J';
char R = 'I';
char S = 'H';
char T = 'G';
char U = 'F';
char V = 'E';
char W = 'D';
char X = 'C';
char Y = 'B';
char Z = 'A';
System.out.println("Enter the message you would like to see in reverse: ");
String message = scanner.nextLine().toUpperCase();
if (new String(message).contains("A")) {
System.out.print(A);
}
if (new String(message).contains("B")) {
System.out.print(B);
}
if (new String(message).contains("C")) {
System.out.print(C);
}
if (new String(message).contains("D")) {
System.out.print(D);
}
if (new String(message).contains("E")) {
System.out.print(E);
}
if (new String(message).contains("F")) {
System.out.print(F);
}
if (new String(message).contains("G")) {
System.out.print(G);
}
if (new String(message).contains("H")) {
System.out.print(H);
}
if (new String(message).contains("I")) {
System.out.print(I);
}
if (new String(message).contains("J")) {
System.out.print(J);
}
if (new String(message).contains("K")) {
System.out.print(K);
}
if (new String(message).contains("L")) {
System.out.print(L);
}
if (new String(message).contains("M")) {
System.out.print(M);
}
if (new String(message).contains("N")) {
System.out.print(N);
}
if (new String(message).contains("O")) {
System.out.print(O);
}
if (new String(message).contains("P")) {
System.out.print(P);
}
if (new String(message).contains("Q")) {
System.out.print(Q);
}
if (new String(message).contains("R")) {
System.out.print(R);
}
if (new String(message).contains("S")) {
System.out.print(S);
}
if (new String(message).contains("T")) {
System.out.print(T);
}
if (new String(message).contains("U")) {
System.out.print(U);
}
if (new String(message).contains("V")) {
System.out.print(V);
}
if (new String(message).contains("W")) {
System.out.print(W);
}
if (new String(message).contains("X")) {
System.out.print(X);
}
if (new String(message).contains("Y")) {
System.out.print(Y);
}
if (new String(message).contains("Z")) {
System.out.print(Z);
}
}
}

答案1

得分: 2

你的方法不起作用,因为你只是检查消息中字母的存在,而完全忽略了它们的位置。

也许像这样更简单:

public static void main(String[] args) {
    System.out.println("输入您想要按相反顺序查看的消息:");

    Scanner scanner = new Scanner(System.in);
    String message = scanner.nextLine().toUpperCase();

    for (char letter: message.toCharArray()) {
        if (letter >= 'A' && letter <= 'Z') {
            letter = (char) ('Z' - (letter - 'A'));
        }
        System.out.print(letter);
    }
    System.out.println();
}
英文:

your approach does not work because you're just checking presence of the letter in message, but completely ignoring its position

probably something like that is simpler:

public static void main(String[] args) {
System.out.println(&quot;Enter the message you would like to see in reverse: &quot;);
Scanner scanner = new Scanner(System.in);
String message = scanner.nextLine().toUpperCase();
for (char letter: message.toCharArray()) {
if (letter &gt;= &#39;A&#39; &amp;&amp; letter &lt;= &#39;Z&#39;) {
letter = (char) (&#39;Z&#39; - (letter - &#39;A&#39;));
}
System.out.print(letter);
}
System.out.println();
}

huangapple
  • 本文由 发表于 2023年2月8日 08:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定