gpt4 book ai didi

java - 如何让java接受一行输入,显示结果,并要求更多输入?

转载 作者:行者123 更新时间:2023-12-01 19:51:52 25 4
gpt4 key购买 nike

我正在创建一个Caesar Cypher程序,我需要让用户可以输入一行,单击回车,计算机将显示结果(同一行,但每个字母移动一定量),然后程序应该能够在下一行接收更多输入,显示结果等。直到连续单击回车键两次。

所需输出的示例:

How many characters do you want to cycle backwards by?: (user inputs integer) 5
Enter a sentence: I hear Canada is a great place to visit.
D czvm Xvivyv dn v bmzvo kgvxz oj qdndo.
Good thing that our school is one of the best schools around!
Bjjy ocdib ocvo jpm nxcjjg dn jiz ja ocz wzno nxcjjgn vmjpiy!

当在空行上单击回车键时,上述程序结束。我是个新手,感谢您的帮助!

代码:https://pastebin.com/eyJyd81W

 System.out.print("How many characters do you want to cycle backwards by?: ");
int cycles = sc.nextInt();

System.out.print("Enter a sentence: ");
String r = sc.nextLine();
String sentence = sc.nextLine();

while (sentence != null) {//Beginning of a while loop.

if (sentence.isEmpty()) { //If the enter key is clicked when the line is empty, the loop breaks.
System.out.println(" ");
break;

}
if (sc.hasNextLine()) { //If the enter key is clicked when there is input in the same line, the computer will go to the next line and scan for more input.

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

int letter = 0;
char l = ' ';
int difference = 0;
int let = 0;

letter = ((int) sentence.charAt(i));
let = letter;

if (let < 91 && let > 64) { //Check if uppercase

if (let - cycles < 65) { //if letters are still valid after subtracting cycles
difference = -(let - 65 - cycles);
let = 91 - difference;
l = (char) let;
System.out.print(l);

} else if ((let - cycles) >= 65) { //For basic backwards cycles (M (shifted back 3) becomes J)

let = letter - cycles;
l = (char) let;
System.out.print(l);
}
} else if (let == 32) { //If a space is entered, this will add it.
System.out.print(" ");
} else if (let > 96 && let < 123) { //If letters are lowercase

if (let - cycles < 97) {
difference = -(let - cycles - 97);
let = 123 - difference;
l = (char) let;
System.out.print(l);
} else if (let - cycles >= 96) {
let = letter - cycles;
l = (char) let;
System.out.print(l);
}
} else { //if special characters are typed
l = (char) let;
System.out.print(l);
}

}
sentence = sc.nextLine();
} else {
sentence = null;
}

}
}
}

我尝试创建一个 while 循环来扫描输入,但唯一的问题是它要求两个输入,显示 2 个结果,并要求在与前 2 个结果相同的行上提供更多输入。如果这有任何意义的话。

最佳答案

你们已经很接近了。这有点简单。

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("How many characters do you want to cycle backwards by?: ");
int cycles = sc.nextInt();

System.out.print("Enter a sentence: ");
sc.nextLine();

while (sc.hasNextLine()) {//Beginning of a while loop.

String sentence = sc.nextLine();

if (sentence.length() == 0)
break;

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

int letter = 0;
char l = ' ';
int difference = 0;
int let = 0;

letter = ((int) sentence.charAt(i));
let = letter;

if (let < 91 && let > 64) { //Check if uppercase

if (let - cycles < 65) { //if letters are still valid after subtracting cycles
difference = -(let - 65 - cycles);
let = 91 - difference;
l = (char) let;
System.out.print(l);

} else if ((let - cycles) >= 65) { //For basic backwards cycles (M (shifted back 3) becomes J)

let = letter - cycles;
l = (char) let;
System.out.print(l);
}
} else if (let == 32) { //If a space is entered, this will add it.
System.out.print(" ");
} else if (let > 96 && let < 123) { //If letters are lowercase

if (let - cycles < 97) {
difference = -(let - cycles - 97);
let = 123 - difference;
l = (char) let;
System.out.print(l);
} else if (let - cycles >= 96) {
let = letter - cycles;
l = (char) let;
System.out.print(l);
}
} else { //if special characters are typed
l = (char) let;
System.out.print(l);
}

}
System.out.println();
}
}
}

关于java - 如何让java接受一行输入,显示结果,并要求更多输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59080973/

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