网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java两数之差代码 java两个数相加代码

JAVA 2差数 代码 有人知道怎么写么

你说的是二叉树吧·····

创新互联公司是一家从事企业网站建设、网站设计、成都网站设计、行业门户网站建设、网页设计制作的专业网站建设公司,拥有经验丰富的网站建设工程师和网页设计人员,具备各种规模与类型网站建设的实力,在网站建设领域树立了自己独特的设计风格。自公司成立以来曾独立设计制作的站点上1000家。

/**

* 二叉树测试二叉树顺序存储在treeLine中,递归前序创建二叉树。另外还有能

* 够前序、中序、后序、按层遍历二叉树的方法以及一个返回遍历结果asString的

* 方法。

*/

public class BitTree {

public static Node2 root;

public static String asString;

//事先存入的数组,符号#表示二叉树结束。

public static final char[] treeLine = {'a','b','c','d','e','f','g',' ',' ','j',' ',' ','i','#'};

//用于标志二叉树节点在数组中的存储位置,以便在创建二叉树时能够找到节点对应的数据。

static int index;

//构造函数

public BitTree() {

System.out.print("测试二叉树的顺序表示为:");

System.out.println(treeLine);

this.index = 0;

root = this.setup(root);

}

//创建二叉树的递归程序

private Node2 setup(Node2 current) {

if (index = treeLine.length) return current;

if (treeLine[index] == '#') return current;

if (treeLine[index] == ' ') return current;

current = new Node2(treeLine[index]);

index = index * 2 + 1;

current.left = setup(current.left);

index ++;

current.right = setup(current.right);

index = index / 2 - 1;

return current;

}

//二叉树是否为空。

public boolean isEmpty() {

if (root == null) return true;

return false;

}

//返回遍历二叉树所得到的字符串。

public String toString(int type) {

if (type == 0) {

asString = "前序遍历:\t";

this.front(root);

}

if (type == 1) {

asString = "中序遍历:\t";

this.middle(root);

}

if (type == 2) {

asString = "后序遍历:\t";

this.rear(root);

}

if (type == 3) {

asString = "按层遍历:\t";

this.level(root);

}

return asString;

}

//前序遍历二叉树的循环算法,每到一个结点先输出,再压栈,然后访问它的左子树,

//出栈,访问其右子树,然后该次循环结束。

private void front(Node2 current) {

StackL stack = new StackL((Object)current);

do {

if (current == null) {

current = (Node2)stack.pop();

current = current.right;

} else {

asString += current.ch;

current = current.left;

}

if (!(current == null)) stack.push((Object)current);

} while (!(stack.isEmpty()));

}

//中序遍历二叉树

private void middle(Node2 current) {

if (current == null) return;

middle(current.left);

asString += current.ch;

middle(current.right);

}

//后序遍历二叉树的递归算法

private void rear(Node2 current) {

if (current == null) return;

rear(current.left);

rear(current.right);

asString += current.ch;

}

}

/**

* 二叉树所使用的节点类。包括一个值域两个链域

*/

public class Node2 {

char ch;

Node2 left;

Node2 right;

//构造函数

public Node2(char c) {

this.ch = c;

this.left = null;

this.right = null;

}

//设置节点的值

public void setChar(char c) {

this.ch = c;

}

//返回节点的值

public char getChar() {

return ch;

}

//设置节点的左孩子

public void setLeft(Node2 left) {

this.left = left;

}

//设置节点的右孩子

public void setRight (Node2 right) {

this.right = right;

}

//如果是叶节点返回true

public boolean isLeaf() {

if ((this.left == null) (this.right == null)) return true;

return false;

}

}

一个作业题,里面有你要的东西。

主函数自己写吧。当然其它地方也有要改的。

java编程中从键盘输入两个数并求其和,差,积,商

import java.util.*;

public class Size{

public static void main(String[] args)throws Exception{

Scanner s= new Scanner(System.in);

System.out.print("请输入第一个数:");

float a=s.nextFloat();

System.out.print("请输入第二个数:");

float b=s.nextFloat();

System.out.println("两个数的和是:"+(a+b));

System.out.println("两个数的差是:"+(a-b));

System.out.println("两个数的积是:"+(a*b));

System.out.println("两个数的商是:"+(a/b));

}

}

用java计算两数的和,差,最大值,最小值并输出

这个是输出菱形的

import java.util.*;

public class lingxing

{

public static void main(String []args)

{

int i,j,k,x,n;

System.out.println("请输入你定义的数值");

Scanner sc=new Scanner(System.in);

n=sc.nextInt();

for(i=0;i=n-1;i++)

{

for(j=0;j=n-i-1;j++)

{

System.out.print(" ");

}

for(k=0;k=2*i;k++)

{

System.out.print("*");

}

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

}

for(i=0;i=n-1;i++)

{

for(j=0;j=i+1;j++)

{

System.out.print(" ");

}

for(k=0;k=2*(n-i-2);k++)

{

System.out.print("*");

}

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

}

}

}

java 求两数和差积商

package com.wdy.reg;

public class Calculator {

private int a;

private int b;

public Calculator(int a,int b){

this.a=a;

this.b=b;

}

public int add(){

return a+b;

}

public int sub(){

return a-b;

}

public int mul(){

return a*b;

}

public int div(){

return a/b;

}

public static void main(String[] args) {

Calculator cal=new Calculator(12, 4);

System.out.println(cal.add());

}

}


当前名称:java两数之差代码 java两个数相加代码
链接地址:http://bjjierui.cn/article/ddjpecj.html

其他资讯