用户输入问题

发布于 2024-08-29 12:13:14 字数 1140 浏览 7 评论 0原文

我的程序检查并测试一个单词或短语是否是回文(向后和向前读起来都相同,例如“赛车”)。我遇到的问题是有人进入“赛车”并对其进行实际测试后。在下面的代码中,我标记了如果我输入“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无名指的心愿 2024-09-05 12:13:14

首先,你给出的代码无法编译;抛开这一点。
看起来您正在寻找的是“如何读取用户输入”,

简单来说,这是您需要做的事情:

import java.util.Scanner;
...
...

Scanner in = new Scanner(System.in);

// Reads a single line from the console 
// and stores into variable called "s"
String s= in.nextLine();
in.close();

然后继续使用代码检查“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:

import java.util.Scanner;
...
...

Scanner in = new Scanner(System.in);

// Reads a single line from the console 
// and stores into variable called "s"
String s= in.nextLine();
in.close();

Then proceed with your code for checking "s" for paliandrome.

短暂陪伴 2024-09-05 12:13:14

一切看起来都不错,但是您的 for 循环仍然显示您正在检查 s 是否是回文,而不是 myInput

现在,您需要做的就是用 myInput 替换所有出现的 s,然后检查您的程序是否正常工作。

请注意,您的程序不会将短语“Madam Im Adam”识别为回文。

由于这是家庭作业,识别短语的一种可能方法(无需更改代码)是删除空格(您可以使用 String#replaceAll) 在字符串中然后进行回文检查。

Everything looks okay, however your for loop still shows you are checking to see if s is a palindrome, not myInput.

Now, all you need to do is replace every occurrence of s with myInput 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文