这是我的代码接受50个名字和滚动号.并按字母顺序打印.它为if(name [j] .compareTo(small))提供错误不兼容的类型
import java .io.*;
class student
{
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name[]=new String[50];
int mark[]=new int[50];
int i;
for( i=0;i<=49;i++)
{
System.out.println("plz ntr d name of d studnt");
name[i]=br.readLine();
System.out.println("plz ntr d marks of d studnt");
mark[i]=Integer.parseInt(br.readLine());
int j,pos=0;
String temp, small;
for(i=0;i<49;i++)
{
small=name[i];
pos=i;
for(j=i+1;j<49;j++)
{
if(name[j].compareTo(small))
pos=j;
}
}
temp=name[i];
name[i]=name[pos];
name[pos]=temp;
}
for(i=0;i<=49;i++)
{
System.out.println((i+1)+" "+name[i]+" "+mark[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
compareTo返回int不是boolean.
你想要的是:
if(name[j].equals(small)) {
Run Code Online (Sandbox Code Playgroud)
编辑另外,你应该检查null:
if (name[j] != null && name[j].equals(small)) {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1994 次 |
| 最近记录: |