我对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);
}
}
2条答案
按热度按时间vlju58qv1#
由于数组的大小最初是未知的,所以可以将每个循环的中间结果加到
ArrayList<Integer>
中,ArrayList<Integer>
的大小可以动态调整,如果需要的话可以使用stream API求和,stream可以引用using java Stream to get a sum, average, and sort中的另一个线程r7xajy2e2#
我相信您可以使用“for”并按如下方式放置索引来完成此操作: