You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何简化这段Java输入与学费折扣计算代码?

Hey there! No worries at all—asking questions is exactly how you learn, and your question is totally clear. Let's break down some practical methods you can use to simplify this code, focusing on the things you mentioned (like handling variables outside blocks) and other beginner-friendly tricks:


1. Extract repeated logic into helper methods

Your code repeats the same discount calculation and print steps in every if/else branch. Instead of writing that code over and over, you can wrap it into a single reusable method. This makes your code cleaner and easier to update later (like if you want to change how the discount is displayed).

For example:

private static void printDiscountedTuition(int discountPercent, double tuitionCost) {
    double discountFactor = (100 - discountPercent) / 100.0;
    double total = tuitionCost * discountFactor;
    System.out.println("Your discount is: " + discountPercent + "% off.");
    System.out.println("Your yearly tuition is: " + total);
}

Then in your conditional branches, you just call this method instead of rewriting the logic:

if(average >= 9.0){
    printDiscountedTuition(20, tuitionCost);
} else if(average >= 8.5){
    printDiscountedTuition(10, tuitionCost);
}
// ... and so on
2. Simplify input handling with reusable prompt methods

Right now, you’re writing System.out.println("Write X: "); variable = s.nextXXX(); for every input. You can turn this into reusable methods too—this cuts down on repetition and fixes a common beginner bug (where nextInt()/nextDouble() leaves a newline that breaks subsequent nextLine() calls).

Example methods:

private static String getStringInput(Scanner scanner, String prompt) {
    System.out.println(prompt);
    return scanner.nextLine();
}

private static int getIntInput(Scanner scanner, String prompt) {
    System.out.println(prompt);
    int input = scanner.nextInt();
    scanner.nextLine(); // Clear the leftover newline
    return input;
}

private static double getDoubleInput(Scanner scanner, String prompt) {
    System.out.println(prompt);
    double input = scanner.nextDouble();
    scanner.nextLine(); // Clear the leftover newline
    return input;
}

Then your input code becomes much cleaner:

String name = getStringInput(s, "Write name: ");
String lastName = getStringInput(s, "Write last name: ");
int id = getIntInput(s, "Write id: ");
double average = getDoubleInput(s, "Write your average GPA: ");
3. Streamline conditional checks

Your current conditionals have redundant checks (like average <= 8.99 && average >=8.5). Since if/else branches run in order, you can simplify these by relying on the fact that earlier conditions already ruled out higher values:

if(average >= 9.0){
    // 20% discount logic
} else if(average >= 8.5){ 
    // This automatically means average <9.0 (since the first condition failed)
    // 10% discount logic
} else if(average >=8.0){
    // Automatically means average <8.5
    // 5% discount logic
} else {
    // No discount
}

This makes your conditions shorter and easier to read.

4. Manage variable scope for reusability

You mentioned wanting to use input variables outside if blocks—great news: you’re already doing this right! Variables like name, lastName, and average are declared outside the conditional blocks, so you can use them anywhere in the method after they’re assigned.

For variables like total and discount values, you can declare them once outside the conditionals and update their values inside the branches. This lets you reuse them later if needed:

double total = tuitionCost; // Default to full price
int discountPercent = 0;

if(average >=9.0){
    discountPercent =20;
} else if(average >=8.5){
    discountPercent=10;
} else if(average >=8.0){
    discountPercent=5;
}

// Now handle all printing/calculation in one place
if(discountPercent >0){
    total = tuitionCost * ((100 - discountPercent)/100.0);
    System.out.println("Your discount is: " + discountPercent + "% off.");
} else {
    System.out.println("You have no discount.");
}
System.out.println("Your yearly tuition is: " + total);

Remember, simplifying code is all about spotting repetition, making logic easier to follow, and breaking big chunks into smaller, reusable pieces. Take it step by step—even small changes will make your code cleaner over time!

内容的提问来源于stack exchange,提问作者alison

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:14:59