0

I have no experience with java at all but its for school project and I'm just confused and have tried to look around for my answer but it just wasnt going anywhere so I'll try asking here. That is what I tried to do. I basically need to get all the values from newgrade and sum it up. I know it has to do something with arrays but I do not know how to do it. itd be very helpful if i could get a clear and simple way of doing it. thank you

import java.util.Scanner;

public class QPI
{
    public static void main(String args[])
    {
        int grade, response,total,qpi;


        Scanner scanner = new Scanner(System.in);

        
    
        System.out.println("Enter your quality point equivalent for each subject:");
        grade = scanner.nextInt();
       

        if (grade == 4 )
        System.out.println("A");
        if (grade == 3.5 )
        System.out.println("B+");
        if (grade == 3 )
        System.out.println("B");
        if (grade == 2.5 )
        System.out.println("C+");
        if (grade == 2 )
        System.out.println("C");
        if (grade == 1 )
        System.out.println("D");
        if (grade == 0 )
        System.out.println("F");

        System.out.println("Enter your total units for this subject:");
        total = scanner.nextInt();

        int newgrade = total*grade;
        System.out.println(newgrade);

        

    

        System.out.println("type 1 to continue adding type 2 to calculate qpi:");
        response = scanner.nextInt();

        if (response == 1);
        do {
            System.out.println("Enter your quality point equivalent for each subject:");
            grade = scanner.nextInt();
            
           
    
            if (grade == 4 )
            System.out.println("A");
            if (grade == 3.5 )
            System.out.println("B+");
            if (grade == 3 )
            System.out.println("B");
            if (grade == 2.5 )
            System.out.println("C+");
            if (grade == 2 )
            System.out.println("C");
            if (grade == 1 )
            System.out.println("D");
            if (grade == 0 )
            System.out.println("F");

            System.out.println("Enter your total units for this subject:");
        total = scanner.nextInt();

         newgrade = total*grade;
        System.out.println(newgrade);



            System.out.println("type 1 to continue adding type 2 to calculate qpi:");
        response = scanner.nextInt(); 
        } while (response != 2);



        if (response == 2);
    
        qpi = newgrade/total;
        System.out.println("Your QPI is:" + qpi);

         
        
    }
}
wraith
  • 1
  • 1
  • https://www.baeldung.com/java-array-sum-average – GabeRAMturn Mar 15 '23 at 14:02
  • I don't see an ARRAY being declared and used anywhere in your code. Also, if "grade" is declared of type `int`, and you are getting an integer with `.nextInt()`, how will it ever be equal to 3.5, which is not an integer?!... – Idle_Mind Mar 15 '23 at 14:33
  • @Idle_Mind hello, sorry its my first time but i do understand what you mean by using the proper datatype and i fixed it. regarding the array I just dont know how to declare it properly thatll work in my code? any suggestion would be helpful thanks – wraith Mar 15 '23 at 14:43
  • For example, here's how to make an array of double that can hold ten doubles: `double[] scores = new double[10];` – Idle_Mind Mar 15 '23 at 14:44
  • @Idle_Mind would you know how I'd make an array variable for "newgrade" so that when the user inputs it will go through the same process and then the first value of the newgrade will then be stored and so on? – wraith Mar 15 '23 at 14:48
  • You need a LOOP to get multiple grades. A `for` loop would make sense here as you can go from 0 to less than the `.length` of the array. – Idle_Mind Mar 15 '23 at 15:44
  • *I have no experience with java at all*. For extra help in learning the language, check out [The Java Tutorials](https://docs.oracle.com/javase/tutorial/index.html) – WJS Mar 16 '23 at 14:15

2 Answers2

0

As the array size is not known initially, you might add the intermediate result of every loop to ArrayList<Integer> which size could adjust dynamically. Then you can use stream api to sum up if need. For stream you could refer to another thread at using java Stream to get a sum, average, and sort

Stone
  • 155
  • 1
  • 6
  • Hello good sir, thank you for this suggestion really helped me out and I finally got it to all workout god bless. – wraith Mar 16 '23 at 14:09
  • "I know it has to do something with arrays" You're allowed to use a `List`, instead of an `Array`, in a beginning Java course?... – Idle_Mind Mar 16 '23 at 14:48
0

I believe you can do this using the "for" and placing indexes as below:

import java.util.Scanner;

public class QPI {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        double grade, total=0;
        int  response;        
        
        System.out.println("Enter how many quality equivalent points for each subject:");
        response = scanner.nextInt();

        double[] newgrade = new double[response];
        double[] qpi = new double[response];

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

            System.out.println("Enter your quality point equivalent for each subject:");
            grade = scanner.nextDouble();

            System.out.println("Enter your total units for this subject:");
            total = scanner.nextInt();
            
            if (grade == 4) {System.out.println("Equivalent to A"); newgrade[i] = total * grade; qpi[i] = newgrade[i]/total;}            
            if (grade == 3.5) {System.out.println("Equivalent to B+"); newgrade[i] = total * grade; qpi[i] = newgrade[i]/total;}            
            if (grade == 3) {System.out.println("Equivalent to B"); newgrade[i] = total * grade; qpi[i] = newgrade[i]/total;}            
            if (grade == 2.5) {System.out.println("Equivalent to C+"); newgrade[i] = total * grade; qpi[i] = newgrade[i]/total;}            
            if (grade == 2) {System.out.println("Equivalent to C"); newgrade[i] = total * grade; qpi[i] = newgrade[i]/total;}            
            if (grade == 1) {System.out.println("Equivalent to D"); newgrade[i] = total * grade; qpi[i] = newgrade[i]/total;}            
            if (grade == 0) {System.out.println("Equivalent to F"); newgrade[i] = total * grade; qpi[i] = newgrade[i]/total;}            
            
        }

        for(double newgrades : newgrade) System.out.println("Newgrade: " + newgrades);
        for(double qpis : qpi) System.out.println("Qpi: " + qpis); 

        scanner.close();    
    }
}