简体   繁体   中英

Sorting integers in descending order without using arrays

I would like to sort the given input of 6 homework grades in descending order now the input is take from a file and it consists of grades in the following format Ex: hw1 hw2 hw3 hw4 hw5 hw6 student 1 50 30 40 10 30 0 student 2 12 20 30 12 30 40 student 3 12 50 60 10 20 30

I have written the proper code to take the input and save the grades in the variables of hw1 which has all hw1 grades for all students and so on and so forth for rest.

I also wrote a code to sort the hw grades but there's some issue with it any help would be appreciated as far as I know there is a problem in my while loop before the sorting code.

 double t2,t1,q1,q2,hw1,hw2,hw3,hw4,hw5,hw6,Final;
 String Last_Name,First_Name;
 int HW_Count=0;

 Scanner input=new Scanner(System.in);
 Scanner inData=new Scanner(System.in);

 File inFile=new File("grades1.txt");
 boolean inFileFound = false;

 try {
     input=new Scanner(inFile);
     inFileFound=true;
 }
 catch(FileNotFoundException fnf) {
     System.out.println("File not found");           
 }

 while(inFileFound&&input.hasNextLine()) {
        Last_Name=input.next();
        t1=input.nextDouble();
        t2=input.nextDouble();
        Final=input.nextDouble();
        hw1=input.nextDouble();
        hw2=input.nextDouble();
        hw3=input.nextDouble();
        hw4=input.nextDouble();
        hw5=input.nextDouble();
        hw6=input.nextDouble();
        q1=input.nextDouble();
        q2=input.nextDouble();
        HW_Count=input.nextInt();
        First_Name=input.nextLine();

        double t1final=t1*10/100;
        double t2final=t2*15/100;
        double Finalfinal=Final*25/200;
        double q1final=q1*1/10;
        double q2final=q2*1/10;
        double finalgrade=0;
        double temp=0;

        while((hw1<hw2)||(hw2<hw3)||(hw3<hw4)||(hw4<hw5)) {
            if(hw1<hw2) {
                temp=hw1;
                hw1=hw2;
                hw2=temp;
            }
            if(hw2<hw3) {
                temp=hw2;
                hw2=hw3;
                hw3=temp;
            }
            if(hw3<hw4) {
                temp=hw3;
                hw3=hw4;
                hw4=temp;
            }
            if(hw4<hw5) {
                temp=hw4;
                hw4=hw5;
                hw5=temp;
            }
            if(hw5<hw6) {
                temp=hw5;
                hw5=hw6;
                hw6=temp;
            }
        }
    }

I found just one issue with your code, albeit it may not be all because it was hard to understand out of context (I'm just going to trust your file reading because I don't know how the file is formatted). Although I don't like using Final as a variable (final is a keyword), it works. The problem I can clearly see is the while loop actually. Though you do account for hw6 in your sort, it's not part of the while loop's if. This means if hw1=5,hw2=10,hw3=15,hw4=20,hw5=25,hw6=0; the sort wouldn't happen and they would still be out of order. Make the while loop while((hw1<hw2)||(hw2<hw3)||(hw3<hw4)||(hw4<hw5))|| (hw5<hw6)) while((hw1<hw2)||(hw2<hw3)||(hw3<hw4)||(hw4<hw5))|| (hw5<hw6)) In addition, even without working with arrays, I would make a swap method to take in 2 hws and swap them because redundant code is extra and messy:

private void swap(double a, double b){
    int temp= a;
    a=b;
    b=temp;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM