两个有趣小题目,大家来决绝下!

yinxianwu 2008-01-29 07:00:47

1) 回文是一种从前向后和从后向前拼写都相同的字符串。编写一个递归方法:testPalindrome,如果数组里存储的字符串是一个回文,则方法返回boolean值为true,否则返回值为false。



2)有一个一维数组charArray,长度为14个字符,编写程序随即从字符 ‘0’到字符‘9’和字符’a’,’b’,’c’,’d’共有14个字符,编写程序随即从字符数组中获取4个字符,组成一个字符串,要求该字符串可以转化为一个整数,如1234满足条件,a234不满足条件。
...全文
241 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ninesea 2008-01-31
  • 打赏
  • 举报
回复
喜欢5楼的代码 3楼的怎么感觉不像递归
velve 2008-01-30
  • 打赏
  • 举报
回复
3楼严谨5楼精密,都值得学习。
artis7811 2008-01-30
  • 打赏
  • 举报
回复
................怎么感觉是我刚刚做过的那道作业........
yinxianwu 2008-01-30
  • 打赏
  • 举报
回复
我自己做的第一题
public class TestHui
{
//用递归判断回文
public boolean testPalindrome(String s)
{
if(s!="")//判断传入的字符是否为空
{
int i= 0,j=s.length()-1;
for(;i<j;i++)
{
for(;j>i;j--)
{
//比较首尾字符字符是否相同
if((s.charAt(i))!=(s.charAt(j)))
{
return false;
}
else
{
//截取字符串首尾后向前一位的子串
String s1=s.substring(i++,j);
testPalindrome(s1); //用testPalindrome(String s)方法递归

}
}
}
return true;
}
else
{
System.out.println("字符串为空!");
return false;
}
}

public static void main(String[] args)
{
TestHui t=new TestHui();
String s="qooggfgq";
if(t.testPalindrome(s))
{
System.out.println(s+":是回文");
}
else
System.out.println(s+":不是回文!");
System.out.println();
}
}


mjgwf 2008-01-30
  • 打赏
  • 举报
回复
	public static void main(String[] args){
int index; //存储charArray数组下标
int num = 0; //存储转换后的数字
String s = ""; //存储随机生成的四位数
char[] charArray ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d'};


for( int i = 0; i < 4; i++){

if(i == 0){ //第一位数不能是字母,否则无法转换成数字
index = (int)(Math.random() * 10);
s = s + charArray[index];
}else{

index = (int)(Math.random() * 14);
s = s + charArray[index];
}

}
System.out.println("字符串为:" + s);
try{
System.out.print("转换后的数字为:");
for(int i =0; i < 4; i++){
num = Integer.parseInt(s.substring(i,i+1)); //将得到的四个数的字符串一位一位转换成数字,遇到字母,结束转换。
System.out.print(num);
}
}catch(NumberFormatException e){
System.exit(1);
}



}


现在看看能看懂了吗?
xiaoben008 2008-01-30
  • 打赏
  • 举报
回复
这是我写的第二题:
public class TestChar{

public static void main(String[] args){
int index;
int num = 0;
String s = "";
char[] charArray ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d'};


for( int i = 0; i < 4; i++){

int k = 0;
if(i == 0){
index = (int)(Math.random() * 10);
s = s + charArray[index];
}else{

index = (int)(Math.random() * 14);
s = s + charArray[index];
}

}
System.out.println("字符串为:" + s);
try{
System.out.print("转换后的数字为:");
for(int i =0; i < 4; i++){
num = Integer.parseInt(s.substring(i,i+1));
System.out.print(num);
}
}catch(NumberFormatException e){
System.exit(1);
}



}


}
有点头晕
看不太懂

楼上的,加个注释好吗
mjgwf 2008-01-30
  • 打赏
  • 举报
回复
int k = 0;

本来打算计数用的,结果没有用上,忘删了,不好意思!
redraiment 2008-01-29
  • 打赏
  • 举报
回复
第一题:
public class Main
{
public static boolean testPalindrome(char[] s, int i, int j)
{
return i >= j? true: s[i] == s[j] && testPalindrome(s, i+1, j-1);
}

public static void main(String[] argv)
{
String s = new String("12321");
System.out.println(testPalindrome(s.toCharArray(), 0, s.length() - 1));
}
}

第二题:不明白你的要求。
Mouse_Knight 2008-01-29
  • 打赏
  • 举报
回复
2楼代码里有个
int k = 0;
。。。能不能解释一下呢?实在看不明白。。。
daniel_kaka 2008-01-29
  • 打赏
  • 举报
回复

public static boolean testPalindrome(Object[] array){
if (array == null || array.length == 0){
return false;
}else if(array.length == 1){
return true;
}else if(array.length == 2){
if(array[0].equals(array[1])){
return true;
}else{
return false;
}
}else{
if (array[0].equals(array[array.length-1])){
Object[] _temp = new Object[array.length - 2];
System.arraycopy(array, 1, _temp, 0, _temp.length);

return testPalindrome(_temp);
}else{
return false;
}
}
}
mjgwf 2008-01-29
  • 打赏
  • 举报
回复
这是我写的第二题:
public class TestChar{

public static void main(String[] args){
int index;
int num = 0;
String s = "";
char[] charArray ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d'};


for( int i = 0; i < 4; i++){

int k = 0;
if(i == 0){
index = (int)(Math.random() * 10);
s = s + charArray[index];
}else{

index = (int)(Math.random() * 14);
s = s + charArray[index];
}

}
System.out.println("字符串为:" + s);
try{
System.out.print("转换后的数字为:");
for(int i =0; i < 4; i++){
num = Integer.parseInt(s.substring(i,i+1));
System.out.print(num);
}
}catch(NumberFormatException e){
System.exit(1);
}



}


}
qiudawei115 2008-01-29
  • 打赏
  • 举报
回复
作业要自己做啊,同志

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧