gpt4 book ai didi

java - 一个简单的java程序

转载 作者:行者123 更新时间:2023-12-01 14:05:34 26 4
gpt4 key购买 nike

我有一个问题陈述

问题

编写一个程序来计算 2 个数字的总和并打印输出。

输入

第 1 行:一个整数。

第 2 行:一个整数。

输出:输出由一个整数组成,该整数对应于总和,后跟一个新行

示例输入 I

3 
1

示例输出 I

4 

示例输入 II

13 
10

示例输出 II

23

我的解决方案是

import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Add {

public static void main(String[] args)throws IOException
{
int a=0, b=0, sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the numbers to be summed");
try{
a=sc.nextInt();
sc.nextLine();
b=sc.nextInt();
}
catch(InputMismatchException e){
System.out.println("Please enter an Integer number");
e.printStackTrace();}
catch(Exception e){System.out.println(e);}

sum=a+b;

System.out.println(sum);
sc.close();
}

}

我应该将它提交到一个在线目录,我假设它会尝试自动执行该程序。当我这样做时,它会告诉我

错误答案差不多了,再想想

我认为在您决定调用增援之前考虑一个小时绰绰有余。

最佳答案

输出应该是“一个与总和相对应的整数,后跟一个新行”。

但是你程序的输出是

Enter the numbers to be summed
<the sum>

关于java - 一个简单的java程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18170990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com