检查连续字母的程序 - Java
A Program that Checks Consecutive Letters - Java
我正在寻求帮助解决我的问题。在学习了大量的 C++ 之后,我们刚刚开始在课程中学习简单的 java。
我们的一项奖励任务是针对那些比 class 中教授的代码更懂代码的人。
任务如下:编写一个名为lettersSeries的函数,它获取用户输入的字母(一次一个字母,假设所有字母都是小写)。一旦用户输入了 3 个连续的字母,该函数将停止接受用户的字母。 (只有for循环可以不用while循环)
示例:a -> b -> a -> c -> d -> e(这里是停止点)
到目前为止,我知道的不多,如果有人能帮助我,我会很高兴...我尝试了一些选项,但我不知道如何追踪字母表,尤其是如何检查字母是否是连续的...
谢谢!
public static void letterSeries() {
//We create a scanner for the input
Scanner letters = new Scanner(System.in);
for(Here I need the for loop to continue the letters input) {
//Here I need to know if to use a String or a Char...
String/Char letter = next.<//Char or String>();
if(Here should be the if statement to check if letters are consecutive) {
/*
Here should be
the rest of the code
I need help with
*/
显然,您可以更改代码,而不是制作我的模式,如果有更简单的方法,我会更开心!
您可以使用 chars 的 Unicode 编号来检查字母是否连续:如果 a 和 b 是您的字母,请尝试检查 b - a == 1 是否。如果以不区分大小写的方式考虑后果('B' 连续到 'a') 然后检查:(b - a == 1 || b - a == 33)。
这是我解决问题的方法,不过我会让你用这个填空,所以我不会为你做所有的作业。
private void letterSeries() {
Scanner scanner = new Scanner(System.in);
char prevChar;
char currChar;
int amountOfConsecutives = 0;
final int AMOUNT_OF_CONSECUTIVES = 2;
for(;;) {
// Take in the users input and store it in currChar
// Check if (prev + 1) == currChar
// If true, amountOfConsecutives++
// If false, amountOfConsecutives = 0;
// If amountOfConsecutives == AMOUNT_OF_CONSECUTIVES
// Break out of the loop
}
}
private void letterSeries() {
Scanner letters = new Scanner(System.in);
String last = "";
int counter = 0;
for (;counter < 2;){
if ( last.equals (last=letters.next())) counter ++;
else counter = 0
}
}
Scanner letters = new Scanner(System.in);
char previousChar = '[=10=]';
int consecutive = 1;
for(; consecutive != 3 ;){
char userInput= letters.findWithinHorizon(".", 0).charAt(0);
if (userInput - previousChar == 1){
consecutive++;
} else {
consecutive = 1;
}
previousChar = userInput;
}
我用这个解决方案作弊了一点。我使用了一个只有中间部分的 for 循环,所以它就像一个 while 循环。
无论如何,这是它的工作原理。
前三行创建了一个用于用户输入的扫描器,一个计算用户连续输入多少个字母的 consecutive
变量,以及一个用于存储前一个字符的 previousChar
。
"Why does consecutive
start at 1?" 你可能会问。那么如果用户输入一个字母,它将与自己连续。
在for循环中,只要consecutive != 3
,代码就会走到运行。循环的第一行我们使用 findWithinHorizon(".", 0).charAt(0)
读取一个字符。然后 if 语句检查它是否与前一个字符是连续的字母。如果是,则将 consecutive
加一,如果不是,则将 consecutive
重置为一。最后,我们将previousChar
设置为userInput
,为下一次迭代做准备。
我正在寻求帮助解决我的问题。在学习了大量的 C++ 之后,我们刚刚开始在课程中学习简单的 java。
我们的一项奖励任务是针对那些比 class 中教授的代码更懂代码的人。
任务如下:编写一个名为lettersSeries的函数,它获取用户输入的字母(一次一个字母,假设所有字母都是小写)。一旦用户输入了 3 个连续的字母,该函数将停止接受用户的字母。 (只有for循环可以不用while循环)
示例:a -> b -> a -> c -> d -> e(这里是停止点)
到目前为止,我知道的不多,如果有人能帮助我,我会很高兴...我尝试了一些选项,但我不知道如何追踪字母表,尤其是如何检查字母是否是连续的...
谢谢!
public static void letterSeries() {
//We create a scanner for the input
Scanner letters = new Scanner(System.in);
for(Here I need the for loop to continue the letters input) {
//Here I need to know if to use a String or a Char...
String/Char letter = next.<//Char or String>();
if(Here should be the if statement to check if letters are consecutive) {
/*
Here should be
the rest of the code
I need help with
*/
显然,您可以更改代码,而不是制作我的模式,如果有更简单的方法,我会更开心!
您可以使用 chars 的 Unicode 编号来检查字母是否连续:如果 a 和 b 是您的字母,请尝试检查 b - a == 1 是否。如果以不区分大小写的方式考虑后果('B' 连续到 'a') 然后检查:(b - a == 1 || b - a == 33)。
这是我解决问题的方法,不过我会让你用这个填空,所以我不会为你做所有的作业。
private void letterSeries() {
Scanner scanner = new Scanner(System.in);
char prevChar;
char currChar;
int amountOfConsecutives = 0;
final int AMOUNT_OF_CONSECUTIVES = 2;
for(;;) {
// Take in the users input and store it in currChar
// Check if (prev + 1) == currChar
// If true, amountOfConsecutives++
// If false, amountOfConsecutives = 0;
// If amountOfConsecutives == AMOUNT_OF_CONSECUTIVES
// Break out of the loop
}
}
private void letterSeries() {
Scanner letters = new Scanner(System.in);
String last = "";
int counter = 0;
for (;counter < 2;){
if ( last.equals (last=letters.next())) counter ++;
else counter = 0
}
}
Scanner letters = new Scanner(System.in);
char previousChar = '[=10=]';
int consecutive = 1;
for(; consecutive != 3 ;){
char userInput= letters.findWithinHorizon(".", 0).charAt(0);
if (userInput - previousChar == 1){
consecutive++;
} else {
consecutive = 1;
}
previousChar = userInput;
}
我用这个解决方案作弊了一点。我使用了一个只有中间部分的 for 循环,所以它就像一个 while 循环。
无论如何,这是它的工作原理。
前三行创建了一个用于用户输入的扫描器,一个计算用户连续输入多少个字母的 consecutive
变量,以及一个用于存储前一个字符的 previousChar
。
"Why does consecutive
start at 1?" 你可能会问。那么如果用户输入一个字母,它将与自己连续。
在for循环中,只要consecutive != 3
,代码就会走到运行。循环的第一行我们使用 findWithinHorizon(".", 0).charAt(0)
读取一个字符。然后 if 语句检查它是否与前一个字符是连续的字母。如果是,则将 consecutive
加一,如果不是,则将 consecutive
重置为一。最后,我们将previousChar
设置为userInput
,为下一次迭代做准备。