Java代码优化求助:新手求改进Minecraft相关Java代码的建议
Hey there! As a fellow Java developer, let's break down how to make your code cleaner, more readable, and more "Java-like"—perfect for a new learner to build good habits early on.
First, let's look at your original code (formatted for clarity):
package minecraft; import java.util.Scanner; class minecraft { public static void main(String[] argv) { Scanner sc = new Scanner(System.in); boolean i = sc.nextBoolean(); boolean isMCagoodgame = i; if (isMCagoodgame == true) { System.out.println("Minecraft is a good game!"); } else { { System.out.println("Minecraft is a NOT good game!"); } } } }
Key Improvements & Suggestions
Follow Java Naming Conventions
Java has standard naming rules that make code easier for everyone to read:- Class names use PascalCase (capitalize the first letter of each word): change
minecrafttoMinecraft - Variable names use camelCase (lowercase first letter, capitalize subsequent words): rename
ito something meaningful likeuserInput, andisMCagoodgametoisMinecraftAGoodGame(or evenisGoodGamesince the context is clear)
- Class names use PascalCase (capitalize the first letter of each word): change
Remove Redundant Code
- You don't need the intermediate variable
i—directly assign the scanner input to your boolean variable:boolean isMinecraftAGoodGame = sc.nextBoolean(); - The extra set of curly braces inside your
elseblock does nothing—delete them to clean up the code.
- You don't need the intermediate variable
Simplify Boolean Condition Checks
When checking a boolean variable, you don't need to compare it totrueexplicitly. Instead ofif (isMinecraftAGoodGame == true), just writeif (isMinecraftAGoodGame)—it's shorter and just as clear.Add Input Guidance
Right now, users won't know what to input! Add a prompt before reading input to make the program user-friendly:System.out.println("Is Minecraft a good game? Please enter true or false:");Properly Close Resources
TheScannerclass uses system resources that should be closed after use. The easiest way (and best practice) is to use a try-with-resources statement, which automatically closes the scanner for you:try (Scanner sc = new Scanner(System.in)) { // all scanner-related code goes here }Handle Potential Input Errors
If a user enters something other thantrueorfalse, your program will throw anInputMismatchException. For a beginner, you can add a simple check or try-catch block to handle this, but even just being aware of this edge case is a good start.
Improved Code Example
Here's what your code looks like with all these tweaks applied:
package minecraft; import java.util.Scanner; class Minecraft { public static void main(String[] args) { System.out.println("Is Minecraft a good game? Please enter true or false:"); try (Scanner scanner = new Scanner(System.in)) { boolean isMinecraftAGoodGame = scanner.nextBoolean(); if (isMinecraftAGoodGame) { System.out.println("Minecraft is a good game!"); } else { System.out.println("Minecraft is NOT a good game!"); } } } }
Extra Tips for New Java Learners
- Get in the habit of formatting your code as you write it—IDEs like IntelliJ IDEA or Eclipse have auto-format shortcuts (Ctrl+Alt+L for IntelliJ, Ctrl+Shift+F for Eclipse) that do this for you.
- Spend a little time learning Java's official naming conventions—they're industry standards and will make your code feel professional even as you're learning.
- Practice writing small programs like this to get comfortable with basic syntax, then gradually move to more complex projects.
内容的提问来源于stack exchange,提问作者Really Real




