JAVA 加法运算

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args){
        int num1,num2,sum;

        Scanner scan = new Scanner(System.in);
        System.out.print("请输入一个整数:");
        num1 = scan.nextInt();
        System.out.print("请输另一个整数:");
        num2 = scan.nextInt();

        sum=num1+num2;
        System.out.print("结果:"+num1+"+"+num2+"="+sum);
    }

}
  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个Java小学算术运算测试程序的示例: ```java import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; public class ArithmeticTestProgram extends Frame implements ActionListener { private Label label1, label2, label3, label4, label5, label6, label7, label8, label9, label10; private TextField textField; private Button button; private ArrayList<String> questions; private ArrayList<String> answers; public ArithmeticTestProgram() { setTitle("小学生四则运算测试系统-郑州轻工业大学-***-开发,****..");//设置标题 setLayout(new GridLayout(12, 1)); label1 = new Label("小学生四则运算测试系统"); label2 = new Label("请输入题目数量:"); textField = new TextField(); button = new Button("开始测试"); button.addActionListener(this); add(label1); add(label2); add(textField); add(button); setSize(400, 300); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { int num = Integer.parseInt(textField.getText()); generateQuestions(num); saveQuestionsToFile(); } } private void generateQuestions(int num) { questions = new ArrayList<>(); answers = new ArrayList<>(); for (int i = 0; i < num; i++) { int operand1 = (int) (Math.random() * 100); int operand2 = (int) (Math.random() * 100); int operator = (int) (Math.random() * 4); String question = ""; String answer = ""; switch (operator) { case 0: question = operand1 + " + " + operand2 + " = "; answer = String.valueOf(operand1 + operand2); break; case 1: question = operand1 + " - " + operand2 + " = "; answer = String.valueOf(operand1 - operand2); break; case 2: question = operand1 + " * " + operand2 + " = "; answer = String.valueOf(operand1 * operand2); break; case 3: question = operand1 + " / " + operand2 + " = "; answer = String.valueOf(operand1 / operand2); break; } questions.add(question); answers.add(answer); } } private void saveQuestionsToFile() { File file = new File("questions.txt"); try { FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); for (int i = 0; i < questions.size(); i++) { String question = questions.get(i); String answer = answers.get(i); String line = question + answer + "\n"; bos.write(line.getBytes()); } bos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { ArithmeticTestProgram program = new ArithmeticTestProgram(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值