IntelliJ IDEA运行Java文件异常:启动其他文件且构建失败求助
所得税计算Java代码
import java.util.Scanner; public class incometax { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter your income in lacs "); float tax=0; float income= sc.nextFloat(); if (income < 2.5f) { System.out.println("You do not need to pay tax"); } else if (income > 2.5f && income <= 5.0f) { tax = tax + 0.0f * (income - 2.5f); System.out.println("You don't need to pay taxes"); } else if (income > 5f && income <= 7.5f) { //tax=tax+0.0f*(income-5f); tax = tax + 0.1f * (income - 5f); } else if (income >= 7.5f && income <= 10f) { //tax=tax+0.0f*(income-2.5f); tax = tax + 0.1f * (income - 5f); tax = tax + 0.15f * (income - 7.5f); } else if (income > 10f && income <= 12.5f) { tax = tax + 0.1f * (income - 5f); tax = tax + 0.15f * (income - 7.5f); tax = tax + 0.2f * (income - 10f); } else if (income > 12.5f && income <= 15f) { tax = tax + 0.1f * (income - 5f); tax = tax + 0.2f * (income - 7.5f); tax = tax + 0.2f * (income - 10f); tax = tax + 0.25f * (income - 12.5f); //tax=tax+0.15f*(income-10f); } else if (income > 15f) { tax = tax + 0.1f * (income - 5f); tax = tax + 0.15f * (income - 7.5f); tax = tax + 0.2f * (income - 10f); tax = tax + 0.25f * (income - 12.5f); tax = tax + 0.3f * (income - 15f); } System.out.println("Your total tax is :" +tax +"lacs"); } }
问题说明
上述代码逻辑无问题,但在IntelliJ IDEA中运行该文件或任意文件时,IDE会自动启动另一个无关文件,同时提示构建失败,求解决办法。
解决步骤
检查运行配置
点击顶部菜单栏Run->Edit Configurations...,在弹出窗口中找到当前使用的运行配置,确认Main class选项里指定的是incometax(你要运行的目标类),而非其他无关类。配置错误的话直接修改保存,再重新运行。清理IDE缓存并重启
点击File->Invalidate Caches...,勾选Clear file system cache and local history,然后选择Invalidate and Restart。缓存损坏是IDE出现奇怪问题的常见诱因,重启后多数异常会修复。检查模块与源代码目录配置
右键项目根目录 ->Open Module Settings(或按快捷键F4),进入Modules标签页,确认你的源代码目录(比如src)被标记为蓝色的Sources。如果标记错误,右键该目录选择Mark Directory as->Sources Root。验证JDK配置
同样在Project Structure窗口(F4),切换到Project标签,确认Project SDK选择的是本地已安装的正确JDK版本,Project language level与JDK版本匹配。未配置JDK的话,点击New->JDK选择本地JDK路径。重新导入项目
若以上方法均无效,删除IDE自动生成的.idea文件夹和所有.iml文件,然后打开IntelliJ IDEA,选择File->Open重新导入项目,让IDE重新生成配置文件。
内容的提问来源于stack exchange,提问作者Ashutosh Kumar

