gpt4 book ai didi

java - 两个数之和

转载 作者:行者123 更新时间:2023-12-02 04:19:38 24 4
gpt4 key购买 nike

我想计算两个数字的和。但我这样做有问题。我不明白为什么我的总和总是为零。

import java.util.*;
public class Numbers {
static int a;
static int b;
static int result;

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Type the first number:");
String a = in.nextLine();
System.out.println(a);
System.out.println("Type the second number:");
String b = in.nextLine();
System.out.println(b);
display();
}

public static void display(){
result=a+b;
System.out.println("Sum of numbers is " + result);
}
}

最佳答案

我不是java程序员,但我可以看到你有名为ab的全局变量以及名为a的局部变量和b

您的main()正在设置局部变量。 display() 正在读取全局变量。

关于java - 两个数之和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32924952/

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