java写个方法 拜托

2025-12-06 06:30:29
推荐回答(2个)
回答1:

//假设你的数是保存在String a中的
//程序是经过运行过的,这个方法绝对没有问题,如果你用这个方法没有运行出来,我向你保证绝对不会这个方法出错,注意我,我是假设你把数字保存在a中,如果没有,方法中你要做相应的改正哦
boolean test(){//你需要的方法
for (int i=0;i for(int j=i+1;j if(a.charAt(i)==a.charAt(j))
{
return false;//如果找到相同的,就跳出循环
}else{}
}
return true;//如果程序能执行到这里,说明没有找到相同的
}

回答2:

public class Test
{
public static void main (String[] args)
{
System.out.print ("n = ");
Scanner in = new Scanner(System.in);
int n = in.nextInt ();
System.out.print (f(n));
}
static boolean f(int n)
{
String s = String.valueOf (n);
char[] c = s.toCharArray ();
for (int i = 0; i < c.length - 1; i++)
for (int j = i + 1; j < c.length; j++)
if (c[i] == c[j])
return false;
return true;
}
}