《JAVA语言》课问题汇总

一、阅读相应教材,或者使用互联网搜索引擎,弄清楚反码、补码跟原码这几个概念,然后编写示例程序,对正数、负数进行各种位操作,观察输出结果,与手工计算的结果进行比对,看看Java中的数是采用上述哪种码表示的。

1. 原码

原码就是符号位加上真值的绝对值, 即用第一位表示符号, 其余位表示值. 比如如果是8位二进制:

[+1]原 = 0000 0001

[-1]原 = 1000 0001

第一位是符号位. 因为第一位是符号位, 所以8位二进制数的取值范围就是:

[1111 1111 , 0111 1111]

[-127 , 127]

原码是人脑最容易理解和计算的表示方式.

2. 反码

反码的表示方法是:

正数的反码是其本身

负数的反码是在其原码的基础上, 符号位不变,其余各个位取反.

[+1] = [00000001]原 = [00000001]反

[-1] = [10000001]原 = [11111110]反

可见如果一个反码表示的是负数, 人脑无法直观的看出来它的数值. 通常要将其转换成原码再计算.

3. 补码

补码的表示方法是:

正数的补码就是其本身

负数的补码是在其原码的基础上, 符号位不变, 其余各位取反, 最后+1. (即在反码的基础上+1)

[+1] = [00000001]原 = [00000001]反 = [00000001]补

[-1] = [10000001]原 = [11111110]反 = [11111111]补

对于负数, 补码表示方式也是人脑无法直观看出其数值的. 通常也需要转换成原码在计算其数值.

二、课堂测试1:像二柱子那样,花二十分钟写一个能自动生成30道小学四则运算题目的 “软件”。

package calculate;

 

public class calculate {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

 

}

 

}

class  Calculat{

public static void main(String[] args) {

for(int i=1;i<=30;i++)

{

System.out.print(i+"");

int x=(int)(Math.random()*100);

System.out.print(x);

int operator=(int)(Math.random()*4+1);

if(operator==1)

{

System.out.print("+");

}

else if(operator==2)

{

System.out.print("-");

}

else if(operator==3)

{

System.out.print("*");

}

else if(operator==4)

{

System.out.print("/");

}

int y=(int)(Math.random()*100);

if(x<y)

{

int temp;

temp=x;

x=y;

y=temp;

}

System.out.println(y);

  }

}

}

三、在“二”的基础上进行课堂测试2: 1、题目避免重复;2、可定制(数量/打印方式);3、可以控制下列参数: 是否有乘除法、是否有括号、 数值范围、加减有无负数、除法有无余数、否支持分数 (真分数, 假分数, …)、是否支持小数 (精确到多少位)、打印中每行的间隔可调整。

package calculate;

import java.util.Random ;

import java.util.Scanner;

import java.lang.reflect.Array;

import java.util.LinkedList;

import java.util.List;

 

public class FourOperations1

{

    public static void main(String[] ages)

    {

        while(1>0)

        {

            System.out.print("\n请选择要求:"

                +"\n  是否有乘除法;(True/False)"

                +"\n  是否有括号(最多可以支持十个数参与运算)(True/False)"

                +"\n  数值范围(True(1~10)/False(1~100))"

                +"\n  加减乘除有无负数(True/False)"

                +"\n  除法有无余数(True/False)");

            Scanner sc = new Scanner(System.in);

            Boolean[] a=new Boolean[5];

            for(int i=0;i<5;i++)

            {

                 a[i]=sc.nextBoolean();

            }

            //sc.close();

            System.out.println("请输入要出题的个数");

            Scanner N = new Scanner(System.in);

            int index=N.nextInt();

            //N.close();

        

            for(int ii=0;ii<index;ii++)

            {    

                if(a[1]==false)

                {

                    int a1=RandomNum(100),a2=RandomNum(100);

                    int sign=RandomNum(4);

                    if(sign==3&&a[4]==false)

                    {

                        a1=(a1%15)*(a2%10);

                        System.out.print("\n"+a1);

                        PrintSign(sign);

                        System.out.print(a2%10+"  =");

                        continue;

                    }

                    if(a[0]==false)

                    {            

                        if(a[3]==false)

                        {

                            if(a[2]==false)

                            {

                                

                                System.out.print("\n"+a1);

                                PrintSign(sign%2);

                                System.out.print(a2+"  =");

                            }    

                            else if(a[2]==true)

                            {

                                System.out.print("\n"+a1%10);

                                PrintSign(sign%2);

                                System.out.print(a2+"  =");

                            }

                        }

                        else if(a[3]==true)

                        {

                            int ssign=RandomNum(2);

                            if(ssign==0){

                                a1=-a1;

                            }

                            if(a[2]==false)

                            {

                                System.out.print("\n"+a1);

                                PrintSign(sign%2);

                                System.out.print(a2+"  =");

                            }    

                            else if(a[2]==true)

                            {

                                System.out.print("\n"+a1%10);

                                PrintSign(sign%2);

                                System.out.print(a2%10+"  =");

                            }

                        }

                    }

                    else if(a[0]==true)

                    {

                        if(a[3]==false)

                        {

                            if(a[2]==false)

                            {

                                System.out.print("\n"+a1);

                                PrintSign(sign);

                                System.out.print(a2+"  =");

                            }    

                            else if(a[2]==true)

                            {

                                System.out.print("\n"+a1%10);

                                PrintSign(sign);

                                System.out.print(a2%10+"  =");

                            }

                        }

                        else if(a[3]==true)

                        {

                            int ssign=RandomNum(2);

                            if(ssign==0){

                                a1=-a1;

                            }

                            if(a[2]==false)

                            {

                                System.out.print("\n"+a1);

                                PrintSign(sign);

                                System.out.print(a2+"  =");

                            }    

                            else if(a[2]==true)

                            {

                                System.out.print("\n"+a1%10);

                                PrintSign(sign);

                                System.out.print(a2%10+"  =");

                            }

                        }

                        

                    }

                }

                else if(a[1]==true)

                {

                    int size=RandomNum(9);

                    int[] array=new int[size*2+4];

                    if(a[0]==false){

                        for(int i=0;i<=size;i++){

                            array[i]=RandomNum(2);

                        }

                    }

                    else if(a[0]==true){

                        for(int i=0;i<=size;i++){

                            array[i]=RandomNum(4);

                        }

                    }

                    if(a[2]==true){

                        for(int i=size+1;i<=size*2+2;i++){

                            array[i]=RandomNum(70)+4;

                        }

                    }

                    else if(a[2]==false){

                        for(int i=size+1;i<=size*2+2;i++){

                            array[i]=RandomNum(96)+4;

                        }

                    }

                    

                    if(a[3]==true){

                        for(int i=size+1;i<=size*2+2;i++){

                            int ssign=RandomNum(2);

                            if(ssign==0){

                            array[i]=-array[i];

                            }

                        }

                    }

                    

                    LinkedList<Node> nodeList = new LinkedList<Node>();

                    for(int nodeIndex=0;nodeIndex<array.length;nodeIndex++){

                         nodeList.add(new Node(array[nodeIndex]));       

                    

                    }

                    creatBinTree(array,nodeList);

                    Node root = nodeList.get(0);

                    inOrderTraverse(root);

                    System.out.println("  =  ");

                }

            }

        }

    }

    public static int RandomNum(int i)

    {

        Random a=new Random();

        int a1=a.nextInt (i);

        return a1;

    }

    public static void PrintSign(int sign)

    {

        if(sign==0){

            System.out.print("  +  ");

        }

        else if(sign==1){

            System.out.print("  -  ");

        }

        else if(sign==2){

            System.out.print("  *  ");

        }

        else if(sign==3){

            System.out.print("  /  ");

        }

        else

            System.out.print(sign);

    }

    

    public static class Node

    {

        Node leftchild;

        Node rightchild;

        int data;

        

        Node(int newData){

            leftchild=null;

            rightchild=null;

            data=newData;

        }

    }

    

    public static void creatBinTree(int array[],LinkedList<Node> nodeList){

        

        

        for(int parentIndex=0;parentIndex<array.length/2-1;parentIndex++){

            nodeList.get(parentIndex).leftchild = nodeList  

                    .get(parentIndex * 2 + 1);

            nodeList.get(parentIndex).rightchild = nodeList  

                    .get(parentIndex * 2 + 2);

        }

    }

    public static void inOrderTraverse(Node node) {  

        if (node == null)  

            return;  

        if (node.leftchild != null) {

        System.out.print("(");  

        }

        inOrderTraverse(node.leftchild);  

        PrintSign(node.data );  

        inOrderTraverse(node.rightchild);

        if (node.rightchild != null) {

            System.out.print(")");  

        }

    }  

    

}

posted @ 2019-09-20 11:43  Spongbob  阅读(181)  评论(0编辑  收藏  举报