删除最低级别的java程序

时间:2013-11-28 17:43:01

标签: java eclipse if-statement

我正在编写一个java程序,它将从键盘4输入读取,名为Test1,Test2,Test3,Final。然后,程序将确定测试1,2和3的3个测试等级中的最佳2(BTest1,BTest2)。然后,它将使用以下评分策略计算最终成绩: BTest1:30%BTest2:30%Final:40%

这是我得到的:

public static void main (String[] args)
    {

        Scanner scan = new Scanner (System.in);


        int Test1 = 0,
                Test2 = 0,
                Test3 = 0,
                Final = 0;


        double AVG;

        // Test 1 input
        System.out.println ("Enter Test 1: ");
        Test1 = scan.nextInt();

        while ((Test1 < 0) || (Test1 > 100))
        {
            System.out.println ("Invalid Input, try again.");
            Test1 = scan.nextInt();

        }

        // Test 2 input
        System.out.println ("Enter Test 2: ");
        Test2 = scan.nextInt();

        while ((Test2 < 0) || (Test2 > 100))
        {
            System.out.println ("Invalid Input, try again.");
            Test2 = scan.nextInt();

        }

        // Test 3 input
        System.out.println ("Enter Test 3: ");
        Test3 = scan.nextInt();

        while ((Test3 < 0) || (Test3 > 100))
        {
            System.out.println ("Invalid Input, try again.");
            Test3 = scan.nextInt();

        }

        // Final Exam input
        System.out.println ("Enter Final Exam: ");
        Final = scan.nextInt();

        while ((Final < 0) || (Final > 100))
        {
            System.out.println ("Invalid Input, try again.");
            Final = scan.nextInt();

        }


        // Find the highest out of the 3 tests
        int BTest1,
            BTest2;

        if ((Test1 >= Test2) && (Test1 >= Test3))
             BTest1 = Test1;
        if ((Test2 >= Test1) && (Test2 >=Test3))
            BTest2 = Test2;
        if ((Test3 >= Test1) && (Test3 >= Test2)) 
            BTest2 = Test3;


        // Compute the Average
        AVG = ((BTest1 * .3) + (BTest2 * .3) + (Final * .4));

                if (AVG >= 90)
                    System.out.println ("A " + AVG); 
                else
                    if (AVG >= 80)
                        System.out.println ("B " + AVG);
                    else
                        if (AVG >= 70)
                            System.out.println ("C " + AVG);
                        else
                            if (AVG >= 60)
                                System.out.println ("D " + AVG);
                            else
                                System.out.println ("F " + AVG);



    }
}

我遇到的问题是我无法降低最低等级。 有人可以请我指导正确的方向吗?

提前谢谢!

2 个答案:

答案 0 :(得分:0)

您的问题是您认为Test1将自动高于Test2或Test3,但如果不是(例如10,50,60),则BTest1将永远不会被设置。要正确执行此操作,您需要一系列非常长的if / else调用。但有一种更简单的方法:

public static void main(String[] args) {
  final Scanner scan = new Scanner(System.in);

  final int[] testScores = new int[3]; // use an array here
  Arrays.fill(testScores, -1); // set all to -1 to know if we already got a proper input

  for (int i = 0; i < testScores.length; ++i) { // for loop simplifies the whole thing a lot
    while ((testScores[i] < 0) || (testScores[i] > 100)) {
      System.out.println("Please enter Test " + (i + 1) + ":");
      testScores[i] = scan.nextInt();
    }
  }   

  int exam = -1; // Final is a bad name, because final is a reserved word
  while ((exam < 0) || (exam > 100)) {
    System.out.println("Please enter Final Exam:");
    exam = scan.nextInt();
  }

  Arrays.sort(testScores); // automatically sorts the entry from lowest to highest
  final double AVG = (testScores[testScores.length - 1] + testScores[testScores.length - 2]) * 0.3 + exam * 0.4; // therefore the best grades are the last two entries

  if (AVG >= 90) {
    System.out.println("A " + AVG);
  } else if (AVG >= 80) {
    System.out.println("B " + AVG);
  } else if (AVG >= 70) {
    System.out.println("C " + AVG);
  } else if (AVG >= 60) {
    System.out.println("D " + AVG);
  } else {
    System.out.println("F " + AVG);
  }
}

答案 1 :(得分:-1)

您可以执行以下操作:

TreeSet<Integer> tests = new TreeSet<Integer>();
tests.add(test1);
tests.add(test2);
tests.add(test3);

if (tests.size() == 1) {
    bTest1 = tests.last();
    bTest2 = tests.poolLast();
} else {
    bTest1 = tests.poolLast();
    bTest2 = tests.poolLast();
}

它将生成一组最多3个元素,然后检索集合中的2个最大元素(当集合中只有1个元素时也处理情况 - 所有插入的值都相同)。

看看Java naming conventions。你的所有变量对我来说都是类。

编辑:更改了bTest1bTest2的地点 - 并不是会改变任何内容(请参阅评论)。

相关问题