You need to enable JavaScript to run this app. Java使用Scanner输入字符串后程序无法继续执行问题求解
优惠活动
大模型
产品
解决方案
定价
更多

Java使用Scanner输入字符串后程序无法继续执行问题求解

问题原因及修复方案

核心问题根源

你遇到的两个异常都是Scanner类的输入逻辑使用错误导致的:

  1. 输入字符串后卡住:你先执行了cin.nextInt()再做合法性校验,当输入非整数内容时,nextInt()会直接抛出输入不匹配异常,且不会消耗缓冲区的非法输入,程序不会进入你写的后续校验分支,直接卡死或崩溃。
  2. 输入合法数值后无法跳转下一个环节:nextInt()只会读取整数内容,不会消耗输入结束后按下的换行符,换行符残留在输入缓冲区,会被后续的输入读取操作直接读到空值,同时你代码中大量多余的cin.next()调用会无故消耗正常输入内容,导致流程错乱。

修复要点

  • 调整校验顺序:所有整数输入前先调用hasNextInt()判断输入是否为合法整数,校验通过后再调用nextInt()读取数值
  • 清理输入缓冲区:每次调用nextInt()后主动调用nextLine()吃掉残留的换行符,避免后续读取字符串时出现空值
  • 删除冗余代码:移除不必要的cin.next()调用,避免正常输入被无故消耗
  • 修正回答判断逻辑:处理输入大小写兼容,同时补充空值判断避免空指针异常

修复后完整代码

import java.util.Scanner;

class Assignment01 {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in); 

        // 读取用户名
        System.out.println("Please enter your name"); 
        String yourName = cin.nextLine(); 

        int height = 0; 
        int base = 0;  

        // 读取三角形高度
        System.out.println("Hello, " + yourName + ". Please enter the height of a right triangle: ");  
        if (cin.hasNextInt()) {
            height = cin.nextInt();
            cin.nextLine(); // 吃掉残留换行符
            if (height < 0 || height > 1000) {
                System.out.println("Invalid value");
                System.exit(0);
            }
        } else {
            System.out.println("Invalid value");
            System.exit(0);
        }

        // 读取三角形底边长
        System.out.println("Now enter the base of the same right triangle");
        if (cin.hasNextInt()) {
            base = cin.nextInt();
            cin.nextLine(); // 吃掉残留换行符
            if (base < 0 || base > 1000) {
                System.out.println("Invalid value");
                System.exit(0);
            }
        } else {
            System.out.println("Invalid value");
            System.exit(0);
        }

        // 输出三角形面积
        System.out.println("The area of this triangle is :"  + ((height * base)/2) ); 

        double radius = 0; 

        // 读取圆半径
        System.out.println("Now enter the radius of the circle: "); 
        if (cin.hasNextInt()) {
            radius = cin.nextInt();
            cin.nextLine(); // 吃掉残留换行符
            if (radius <= 0 || radius >= 1000) {
                System.out.println("Invalid value");
                System.exit(0);
            }
        } else {
            System.out.println("Invalid value");
            System.exit(0);
        }

        // 输出圆面积和周长
        System.out.println("The area of the circle is " + Math.PI * (Math.pow(radius, 2)) );
        System.out.println("The perimeter of the circle is " + 2 * Math.PI * radius ); 

        // 读取用户回答
        System.out.println("Can the triangle cover the circle?"); 
        String answer = cin.nextLine().trim(); 

        double triangleArea = ((height * base)/2); 
        double circleArea = Math.PI*(Math.pow(radius, 2));

        // 修正回答判断逻辑
        if (!answer.equalsIgnoreCase("Yes") && !answer.equalsIgnoreCase("No")) {
            System.out.println("Enter yes or no."); 
        } else if ((answer.equalsIgnoreCase("No") && triangleArea >= circleArea) || (answer.equalsIgnoreCase("Yes") && triangleArea <= circleArea)) {
            System.out.println("That is wrong."); 
        } else {
            System.out.println("That is correct.");
        }

        cin.close(); 
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.11 18:27:02