java 如何获取变量的值并将其存储到数组中以便求和?

bq9c1y66  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(156)

我对java一点经验都没有,但它是学校的项目,我只是困惑,并试图寻找我的答案,但它只是没有去任何地方,所以我会尝试在这里问。这是我试图做的。我基本上需要从newgrade获得所有的值,并总结它。我知道它必须做一些数组,但我不知道如何做到这一点。如果我能得到一个简单明了的方法,那将是非常有帮助的。谢谢

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);

         
        
    }
}
vlju58qv

vlju58qv1#

由于数组的大小最初是未知的,所以可以将每个循环的中间结果加到ArrayList<Integer>中,ArrayList<Integer>的大小可以动态调整,如果需要的话可以使用stream API求和,stream可以引用using java Stream to get a sum, average, and sort中的另一个线程

r7xajy2e

r7xajy2e2#

我相信您可以使用“for”并按如下方式放置索引来完成此操作:

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();    
    }
}

相关问题