JAVA简单算数运算

本文档详细介绍了JAVA编程中算术运算符、基本类型转换、自增自减、幂运算以及逻辑运算符的使用,包括整数、浮点数和不同类型间的运算规则,以及Java如何借助Math库进行复杂运算。同时涵盖了短路逻辑和位操作的示例。
摘要由CSDN通过智能技术生成

JAVA支持的运算

算数运算符

+, - , * , /,自增(++),自减(–)

package operator;

public class Demo03 {
    public static void main(String[] args) {
        //自增 ++  自减--
        int a = 3;
        int b = a++;//先将a赋值给b,再自增

        System.out.println(a);//此时a=4
        System.out.println(b);

        int c = ++a;//先自增,再赋值给c

        System.out.println(a);
        System.out.println(c);

        //幂运算  2^3  很多运算会借助Math的工具来做,比如pow的幂运算
        double pow = Math.pow(2, 3);
        System.out.println(pow);


    }
}

简单的结果类型转换

package operator;

public class Demo01 {
    public static void main(String[] args) {
        //需要两个数的运算叫二元运算符
        int a = 10;//Ctrl+D复制当前行到下一行
        int b = 20;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);//a和b是int型所以结果取了整数部分
        System.out.println(a/(double)b);
        System.out.println((double) a/b);
        System.out.println((double) (a/b));//这个结果是(a/b)之后再转的double
    }
}

package operator;

public class Demo02 {
    public static void main(String[] args) {
        Long a = 4008123123L;
        int b = 123;
        short c  = 10;
        byte d = 8;


        System.out.println(a+b+c+d);//结果为Long型
        System.out.println(b+c+d);//结果为int型
        System.out.println(c+d);//结果还是int型(除了Long以外,其它类型的结果都为int型)

    }
}

自增和自减 外加幂运算

package operator;

public class Demo04 {
    public static void main(String[] args) {
        int a = 3;
        int b = a++;//先将a赋值给b后,a再++

        System.out.println(a);
        System.out.println(b);

        int c = ++a;//先将a自增后,再赋值给c
        System.out.println(c);


        //幂运算   2^3   很多特殊运算会使用一些工具来做,比如pow
        double pow = Math.pow(2, 3);//Alt+回车
        System.out.println(pow);

    }
}

与或非

package operator;

public class Demo05 {
    public static void main(String[] args) {
        //&&(与)  ||(或)   !(非)
        boolean a = true;
        boolean b = false;

        System.out.println(a&&b);
        System.out.println(a||b);
        System.out.println(!(a&&b));


        //短路问题    &&如果前面那个已经是false了,后面就不用执行了
        int c = 3;
        boolean d = (c>4 && c++<10);
        System.out.println(c);
        System.out.println(d);

        int e = 3;
        boolean f = (e++>3 && e<2);
        System.out.println(e);
        System.out.println(f);

        /*主要在二进制的运算
        A = 0011 1100
        B = 0000 1101
        ----------------
        A&B = 0000 1100   同时为真则为真
        A|B = 0011 1101   有1个为真则为真
        A^B = 0011 0001   一真一假则为真
        ~B = 1111 0010    取反

        ---------------
        效率极高的左移和右移
        <<  相当于乘以2   0000 0001 左移一位 0000 0010
        >>  相当于除以2   同上
        */
        System.out.println(2<<3);
    }
}

以下是一个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(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值