簡體   English   中英

刪除最低等級的Java程序

[英]Drop the lowest grade java program

我正在編寫一個Java程序,該程序將從鍵盤4個輸入讀取,分別名為Test1,Test2,Test3,Final。 然后,程序將在測試1、2和3的3個測試等級中確定最佳2個等級(BTest1,BTest2)。然后,它將使用以下評分策略計算最終等級:BTest1:30%BTest2:30%決賽: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);



    }
}

我遇到的問題是我無法降低最低成績。 有人可以朝正確的方向引導我嗎?

先感謝您!

您的問題是您假設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);
  }
}

您可以執行以下操作:

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命名約定 在我看來,所有變量都像類。

編輯:更改了bTest1bTest2 -並非它會更改任何內容(請參見評論)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM