编程java来确定一个对称的单词 [英] Programming java to determine a symmetrical word

查看:198
本文介绍了编程java来确定一个对称的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,但我很难弄清楚如何编写代码来确定单词的输入并查看第一个是否与单词的结尾匹配。你可以输入abba并得到答案它是均匀对称的,aba是奇怪的对称。



请告诉我如何:(



只有两个主要内容。



第一次
我想知道它是否是奇怪或均匀的字母数量(字母数量)除以2,如果它以0.5结尾,则奇怪对称,
如果是一个整数,它是均匀对称的。



second
我想得到(即1 = n,2 = n-1,3 = n-2 ......)这个词中的字母位置是执行的主要想法。如果有最后一个字母在奇怪对称的单词中,忽略最后剩下的一个字母。



我感谢任何头脑或想法:)谢谢!



<感谢KDiTraglia,我制作了代码并进行了编译,这就是我的内容。我没有进一步。



报告的问题:



线程main中的异常java.lang.Error:未解决的编译问题:
reverse无法解析或不是字段
反向无法解析或不是字段
语法错误,插入)语句完成IfStatement



这是什么我来自,KDiTraglia的帮助

 公共类WordSymmetric {
public static void main(String [] args){
String word =abccdccba;


if((word.length()%2)== 1){
System.out.println(它们奇怪对称);
// odd
}
else {
System.out.println(它们是均匀对称的);
//偶数
}

int halfLength = word.length()/ 2;
String firstHalf = word.substring(0,halfLength);
String secondHalf = word.substring(halfLength,word.length());

System.out.println(secondHalf.reverse());

if(firstHalf.equals(secondHalf.reverse()){
System.out.println(他们匹配);
//他们匹配
}

}
}

解决方案

您可以使用模运算符来确定单词是偶数还是奇数(java中为%)

  if((word.length%2)== 1){
// odd
}
else {
// even
}

然后将字符串分成两半并比较结尾与正面的反向

  int halfLength = word.length / 2; 
String firstHalf = word.substring(0,halfLength);
String secondHalf = word.substring(halfLength,word.length);
if(firstHalf.equals(secondHalf.reverse()){
//匹配
}

这样的东西应该可行,我只是快速写完了,可能需要做一些改动来匹配java语法。


I am new here, but I am having hard time figuring out how to write a code to determine an input of word and see if the first is matching with the end of the word. You may input abba and get answer it's evenly symmetric and aba is oddly symmetric.

Please show me how:(

Just two main things.

first I want to know if it's oddly or evenly amount of letter(number of letter divided by 2,if it's ending with 0.5, it's oddly symmetric, if is an integer it's evenly symmetric.

second I want to get (i.e 1=n,2=n-1,3=n-2...) position of the letter in the word to be the main idea of the execution.If there is a last letter in the oddly symmetric word, ignore the last remaining letter.

I appreciate any headstart or idea:) Thanks!

Thanks KDiTraglia, I made the code and compiled and here is what I put. I am not getting any further.

Reported problem:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: reverse cannot be resolved or is not a field reverse cannot be resolved or is not a field Syntax error, insert ") Statement" to complete IfStatement

This is what i got from, KDiTraglia's help

public class WordSymmetric {
public static void main(String[] args) {
 String word = "abccdccba";


if ( (word.length() % 2) == 1 ) {
    System.out.println("They are oddly symmetric");
    //odd
}
else {
    System.out.println("They are evenly symmetric");
    //even
}

int halfLength = word.length() / 2;
String firstHalf = word.substring(0, halfLength);
String secondHalf = word.substring(halfLength, word.length());

System.out.println(secondHalf.reverse());

if (firstHalf.equals(secondHalf.reverse()) {
    System.out.println("They match");
    //they match
} 

} }

解决方案

You can use the modulo operator to determine whether the word has an even or odd number of letters (% in java)

if ( (word.length % 2) == 1 ) {
    //odd
}
else {
    //even
}

then just split the string in half and compare the reverse of the end with the front

int halfLength = word.length / 2;
String firstHalf = word.substring(0, halfLength);
String secondHalf = word.substring(halfLength, word.length);
if (firstHalf.equals(secondHalf.reverse()) {
    //they match
}

something like this should work, I just wrote it up real quick, might need to make a few changes to match java syntax.

这篇关于编程java来确定一个对称的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆