这段代码正在创建一个循环,名为“Number of students”的变量不断增加。我找不到问题出在哪里。 {1.14,1.49,1.54,1.86,2.04,2.19,2.86,3.02,3.16} 当我输出它时它应该返回 1
公共课主要{
public static void main(String[] args) {
int ogrenciSayisi=0;
if (Student.readCSV("siraliogrenci.txt").length!=0) {
Student[] student = Student.readCSV("siraliogrenci.txt");
int alt_indis=0;
int ust_indis=student.length-1;
int orta_indis;
while(alt_indis<=ust_indis){
orta_indis=(alt_indis+ust_indis)/2;
if (student[orta_indis].getGPA()==3.02) {
ogrenciSayisi++;
int solIndis = orta_indis-1;
int sagIndis = orta_indis+1;
while (solIndis>=0 && student[solIndis].getGPA()==3.02) {
ogrenciSayisi++;
solIndis--;
}
while (sagIndis<student.length && student[sagIndis].getGPA()==3.02) {
ogrenciSayisi++;
sagIndis++;
}
}
else if(student[orta_indis].getGPA()<3.02){
alt_indis=orta_indis+1;
}
else{
ust_indis=orta_indis-1;
}
}
}
System.out.println(ogrenciSayisi);
}
}
我相信您需要在第一个
alt_indis
.中更改
ust_indis
或
if (student[orta_indis].getGPA()==3.02)
否则,如果它进入那个初始的 if 那么你将保持这个表达式的相同值:
orta_indis=(alt_indis+ust_indis)/2;
在你的情况下
if(student[orta_indis].getGPA()==3.02)
你永远不会更新alt_indis
或ust_indis
所以如果它是真的它在你的while循环中永远是真的while(alt_indis<=ust_indis)
并且永远不会退出循环,我假设这是你的问题。