用户输入问题
我的程序检查并测试一个单词或短语是否是回文(向后和向前读起来都相同,例如“赛车”)。我遇到的问题是有人进入“赛车”并对其进行实际测试后。在下面的代码中,我标记了如果我输入“racecar”并运行,Java 将返回正确答案的位置,因此我知道我就在那里。但就将其输入控制台而言,我缺少什么。我认为我的代码没问题,但也许我缺少某些东西或在错误的位置?除非我遗漏了一些东西,否则并不是真正在寻找新的答案,但如果可能的话,也许是专业人士将我的代码移动到正确的区域,因为我被困住了!
import java.util.*;
public class Palindrome {
public static void main(String[] args) {
String myInput;
Scanner in = new Scanner(System.in);
System.out.println("Enter a word or phrase: "); **//this asks user for input but doesn't check for whether or not it is a palindrome**
myInput = in.nextLine();
in.close();
System.out.println("You entered: " + myInput);
}
{
String s="racecar"; **//I can type a word here and it works but I need**
int i; **//I need it to work where I ask for the input**
int n=s.length();
String str="";
for(i=n-1;i>=0;i--)
str=str+s.charAt(i);
if(str.equals(s))
System.out.println(s+ " is a palindrome");
else System.out.println(s+ " is not a palindrome"); }
}
我是编程新手,所以我希望我得到的东西没问题。我知道回文测试有效,我只需要帮助它通过我将其输入控制台的位置进行测试。谢谢
My program checks to test if a word or phrase is a palindrome (reads the same both backward and forward, ex "racecar"). The issue I'm having is after someone enters in "racecar" getting it to actually test. In the below code, I marked where if I type in "racecar" and run, Java returns the correct answer so I know I'm right there. But what am I missing as far as entering it into the console. I think my code is ok, but maybe I have something missing or in the wrong spot? Not really looking for a new answer unless I'm missing something, but if possible perhaps a pro at this moving my code to the correct area bc I'm stuck!
import java.util.*;
public class Palindrome {
public static void main(String[] args) {
String myInput;
Scanner in = new Scanner(System.in);
System.out.println("Enter a word or phrase: "); **//this asks user for input but doesn't check for whether or not it is a palindrome**
myInput = in.nextLine();
in.close();
System.out.println("You entered: " + myInput);
}
{
String s="racecar"; **//I can type a word here and it works but I need**
int i; **//I need it to work where I ask for the input**
int n=s.length();
String str="";
for(i=n-1;i>=0;i--)
str=str+s.charAt(i);
if(str.equals(s))
System.out.println(s+ " is a palindrome");
else System.out.println(s+ " is not a palindrome"); }
}
I'm new at programming so I'm hoping what I got is ok. I know the palindrome test works I'm just needing helping having it test thru where I'm entering it into the console. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,你给出的代码无法编译;抛开这一点。
看起来您正在寻找的是“如何读取用户输入”,
简单来说,这是您需要做的事情:
然后继续使用代码检查“s”是否为回文。
First of all, the code you have given will not compile; that aside.
Looks like what you are looking for is to "how to read user input"
Here is what you need to do in simple terms:
Then proceed with your code for checking "s" for paliandrome.
一切看起来都不错,但是您的
for
循环仍然显示您正在检查s
是否是回文,而不是myInput
。现在,您需要做的就是用
myInput
替换所有出现的s
,然后检查您的程序是否正常工作。请注意,您的程序不会将短语“Madam Im Adam”识别为回文。
由于这是家庭作业,识别短语的一种可能方法(无需更改代码)是删除空格(您可以使用
String#replaceAll
) 在字符串中然后进行回文检查。Everything looks okay, however your
for
loop still shows you are checking to see ifs
is a palindrome, notmyInput
.Now, all you need to do is replace every occurrence of
s
withmyInput
and then check to see if your program works correctly.As a heads up, your program will not recognize the phrase "Madam Im Adam" as a palindrome.
Since this is homework, one possible approach to recognize phrases (without altering your code) is to remove spaces (you could use
String#replaceAll
) in your string and then perform the palindrome check.