正整数ab的编程怎么写

时间:2025-01-27 05:37:17 网络游戏

C语言

```c

include

int main() {

int a, b, sum;

printf("请输入两个正整数:\n");

scanf("%d %d", &a, &b);

sum = a + b;

printf("%d + %d = %d\n", a, b, sum);

return 0;

}

```

C++

```cpp

include

int main() {

int a, b, sum;

std::cout << "请输入两个正整数:" << std::endl;

std::cin >> a >> b;

sum = a + b;

std::cout<< a << " + "<< b << " = " << sum << std::endl;

return 0;

}

```

Python

```python

a = int(input("请输入正整数A: "))

b = int(input("请输入正整数B: "))

sum = a + b

print(f"{a} + {b} = {sum}")

```

Java

```java

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("请输入正整数A: ");

int a = scanner.nextInt();

System.out.print("请输入正整数B: ");

int b = scanner.nextInt();

int sum = a + b;

System.out.println(a + " + " + b + " = " + sum);

}

}

```

JavaScript (Node.js)

```javascript

const readline = require('readline');

const rl = readline.createInterface({

input: process.stdin,

output: process.stdout

});

rl.question('请输入正整数A: ', function(a) {

rl.question('请输入正整数B: ', function(b) {

const sum = parseInt(a) + parseInt(b);

console.log(`${a} + ${b} = ${sum}`);

rl.close();

});

});

```

这些代码示例均假设输入是两个正整数,并在输入不符合要求时输出`?`。你可以根据具体需求选择合适的编程语言和输入输出方式。