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

Java中如何用sum方法处理int与float混合输入的求和问题?

问题描述

当前代码在第一个输入为float/int、第二个输入为int时能正常求和,但第一个输入为int、第二个输入为float时无法运行:

  • 有效场景:
    输入第一个数字:34.6 → 提示该输入为float类型
    输入第二个数字:90 → 输出结果:124.6
  • 无效场景:
    输入第一个数字:90
    输入第二个数字:34.7 → 程序无法正常运行

原代码

import java.util.Scanner;

// Basic Intro/ Refresher for Sums using User Input
public class BasicSums {

    public static void main(String[] args) {
        
        // User input 
        Scanner input = new Scanner (System.in);
        System.out.println("Enter the first number: ");
        
        
        if (input.hasNextInt()) {
            int initial = input.nextInt();
            System.out.println("\nEnter the second number: ");
            int second = input.nextInt();
            
            System.out.println("Your answer is: " + Integer.sum(initial, second));
        
        //Considering the possibility of the user entering a decimal value  
        } else if (input.hasNextFloat()) {
            System.out.println("This input is of type float.\n");
            float first = input.nextFloat();
            System.out.println("Enter the second number: ");
            float second = input.nextFloat();   
            System.out.println("Your answer is: " + Float.sum(first, second));          
        } 
    }
}

问题原因

原代码逻辑存在局限性:当第一个输入是int时,强制用input.nextInt()读取第二个输入,若此时输入float,会触发InputMismatchException导致程序崩溃;而第一个输入是float时,nextFloat()可以兼容读取int(int可自动转为float),所以该场景正常。

修复方案

统一用float类型处理两个输入,兼容任意顺序的int和float输入,同时保留输入类型的提示逻辑:

import java.util.Scanner;

public class BasicSums {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter the first number: ");
        float first;
        // 判断第一个输入类型并给出提示
        if (input.hasNextInt()) {
            System.out.println("This input is of type int.\n");
            first = input.nextInt(); // int自动转换为float
        } else if (input.hasNextFloat()) {
            System.out.println("This input is of type float.\n");
            first = input.nextFloat();
        } else {
            System.out.println("Invalid input type, please enter a number.");
            input.close();
            return;
        }
        
        System.out.println("Enter the second number: ");
        float second;
        // 第二个输入兼容int和float
        if (input.hasNextInt()) {
            second = input.nextInt();
        } else if (input.hasNextFloat()) {
            second = input.nextFloat();
        } else {
            System.out.println("Invalid input type, please enter a number.");
            input.close();
            return;
        }
        
        System.out.println("Your answer is: " + Float.sum(first, second));
        input.close();
    }
}

说明

  • 用float统一存储输入值,int转float不会丢失精度,确保所有数字输入都能正确计算
  • 对每个输入单独做类型判断,支持任意顺序的int和float输入
  • 增加非法输入的处理逻辑,避免非数字输入导致程序崩溃

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 11:55:08