创建一个在不构建反转的情况下测试回文的程序(Java)
我正在尝试为我的AP计算机科学课程创建一个Java程序,该程序要求用户输入一个字符串,并测试该字符串是否为回文。老师特别告诉我们不要通过构建字符串的反转并测试它是否匹配来完成项目。我知道如何做,同时建立逆转,但我想不出另一种方法来做创建一个在不构建反转的情况下测试回文的程序(Java),java,Java,我正在尝试为我的AP计算机科学课程创建一个Java程序,该程序要求用户输入一个字符串,并测试该字符串是否为回文。老师特别告诉我们不要通过构建字符串的反转并测试它是否匹配来完成项目。我知道如何做,同时建立逆转,但我想不出另一种方法来做 import java.util.*; public class mondayassignment { public static void main(String args[]) { Scanner scan = new Sc
import java.util.*;
public class mondayassignment
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a String: ");
String s = scan.nextLine();
while(s.indexOf(" ") >= 0)
{
s = s.substring(0, s.indexOf(" ")) +
s.substring(s.indexOf(" ") + 1);
}
System.out.println("Spaces Removed:");
System.out.println(s);
String reverse="";
for(int pos = 0; pos < s.length(); pos++)
{
reverse = s.substring(pos, pos + 1) + reverse ;
}
System.out.println("Reverse Of The String You Inputed Is:" );
System.out.println(reverse);
System.out.println("Testing For Palindrome:");
if (reverse.equals(s))
{
System.out.println(s + " " + "is a palindrome" );
}
else {
System.out.println(s + " " + "is not a palindrome" );
}
}
}
import java.util.*;
公共课周一作业
{
公共静态void main(字符串参数[])
{
扫描仪扫描=新扫描仪(System.in);
System.out.print(“输入字符串:”);
字符串s=scan.nextLine();
而(s.indexOf(“”>=0)
{
s=s.substring(0,s.indexOf(“”)+
s、 子字符串(s.indexOf(“”+1);
}
System.out.println(“删除空格:”);
系统输出打印项次;
字符串反向=”;
对于(int pos=0;pos
有人能帮我创建一个代码,它不需要建立字符串的反转吗?我在网上找到的许多解决方案都使用布尔值和字符,但问题是,我还没有学会它们。我只学过双整数和字符串。那么,有没有一种不使用布尔和字符的方法呢?我认为我的老师也不希望我们使用boolean和char,因为我们从未讨论过它们 比较从头和尾开始的字符串char。如果所有字母在字符串中间都相等,则为回文
boolean palindrome(string str){
for(int i = 0 ; i < str.lenght()/2 ; i++) {
if (str.charAt(i) != str.charAt(str.lenght() - i )
return false;
}
return true;
}
布尔回文(字符串str){
对于(int i=0;i
当你目视确认一个单词是否为回文时,通常你只需检查字符的两端,然后开始向内移动
实际上,这是两个步骤:
package-paintadvance;
导入java.util.Scanner;
类回文_不带_反转{
公共静态void main(字符串[]ishan_vimukthi_是我的名字){
扫描仪扫描=新扫描仪(System.in);
字符串=”;
/*当循环继续运行程序时,未点亮的用户需要它结束*/
while(true){
System.out.println(“为回文检查输入字符串”);
string=scan.nextLine();
/*这用于退出程序。如果用户需要,只需键入
“退出”。不区分大小写,表示“退出”也可以。*/
if(string.equalsIgnoreCase(“exit”){System.out.println(“程序关闭”);System.exit(0);}
/*获取整型变量的字符串长度*/
int length=string.length();
/*获取eg中字符串的中点:-“aasaa”中点是字母“s”,表示从0开始计数时为2。
我们可以通过将字符串长度除以2得到它。因为我们使用int,所以我们将得到2作为答案。*/
int中点=长度/2;
布尔值isAlindrome=true;
字符串x=“”;
字符串y=“”;
/*检查长度为奇数或偶数,例如:-“aasaa”为奇数“aassaa”为偶数*/
如果(长度%2==1){
/*如果您想查看该方法,请取消注释并理解它。
System.out.println(“奇数字符串”);
System.out.println(string.substring(中点,string.length())+“X部分”);
System.out.println(string.substring(0,中点+1)+“Y部分”)*/
x=string.substring(中点,string.length());
y=字符串。子字符串(0,中点+1);
}否则{
/*如果您想查看该方法,请取消注释并理解它。
System.out.println(“偶数字符串”);
System.out.println(string.substring(中点,string.length())+“X部分”);
System.out.println(string.substring(0,中点)+“Y部分”)*/
x=string.substring(中点,string.length());
y=字符串。子字符串(0,中点);
}
int ylength=y.长度()-1;
int ylength_end=y.长度();
aa:for(int i=0;i
如果你还没有学到一些东西,也许现在是开始的时候了boolean
和char
并不复杂,它们是非常简单的基本类型,它们是用于
package paintadvance;
import java.util.Scanner;
class Palindrome_without_reverse{
public static void main(String[] ishan_vimukthi_is_my_name) {
Scanner scan= new Scanner(System.in);
String string="";
/*while loop to keep running the programme unlit user need it to end*/
while (true) {
System.out.println("Enter string for palindrome check");
string=scan.nextLine();
/*this is for exiting the programme. If needed for user just type
"exit".It's not casesensitive that means "EXIT" is also ok.*/
if (string.equalsIgnoreCase("exit")) {System.out.println("Programme closed"); System.exit(0); }
/*get the length of string to a int variable */
int length=string.length();
/*get mid point of string in eg:- "aasaa" midpoint is letter "s" that means 2 when count from 0 .
We can get it from dividing string length from 2.Because we use int we will get 2 as the answer. */
int midpoint=length/2;
boolean ispalindrome=true;
String x="";
String y="";
/*check length is odd or even eg:- "aasaa" is odd "aassaa" is even*/
if (length%2==1) {
/*if you want to see the method. uncomment this and understand it.
System.out.println("odd string");
System.out.println(string.substring(midpoint,string.length())+" X part");
System.out.println(string.substring(0, midpoint+1)+" Y part");*/
x =string.substring(midpoint,string.length());
y=string.substring(0, midpoint+1);
}else{
/*if you want to see the method. uncomment this and understand it.
System.out.println("even string");
System.out.println(string.substring(midpoint,string.length())+" X part");
System.out.println(string.substring(0, midpoint)+" Y part");*/
x=string.substring(midpoint,string.length());
y=string.substring(0, midpoint);
}
int ylength=y.length()-1;
int ylength_end=y.length();
aa: for (int i = 0; i < x.length(); i++) {
/* by this code we compare each
string part of both x and y strings
in eg:- aasaa x=saa y=aas
we compair 0,1 of x and 2,3 of y
and keep going.this is not a reversal but this is comparing x and y parts
if its not equal we assign ispalindrome false if its equal it will be in true state
*/
if ( !(x.substring(i, i+1).equals( y.substring(ylength--, ylength_end--)))) {
ispalindrome=false;
}
}
/* after for loop We check string is a palindrome or not */
if (ispalindrome) { System.out.println("This is a palindrome");}else{ System.out.println("Not a palindrome");}
}
}
}