快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java代码让两个数相乘,两个数相乘的代码

怎么用Java把输入的两串字符串中的数字提取出来,并且将两串数字相乘输出?

解决方案:

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的开原网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

使用正则表达式抽取数字子串;

使用Interger.parse将数字子串转为整数类型;

计算两个数字相乘即可;

java编程,两个p进制数相乘

import java.util.Vector;

/**

* P进制数

*

* @author Jarod Yv

*/

public final class P_NUM {

/** 数制 */

public int base = 0;

/** 数字。以Integer的形式保存在向量中 */

public VectorInteger num = null;

/** 数字间的分割符 */

private static final String SEP = ":";

public P_NUM(int base, String num) throws Exception {

init(base, num);

}

public P_NUM(int base, int num) throws Exception {

init(base, num);

}

/**

* 初始化一个P进制数

*

* @param base

* 数制 必须为2的整数

* @param num

* 数字 这里规定每一位之间用:号分割

* @throws Exception

*/

public void init(int base, String num) throws Exception {

// 设置数制

setBase(base);

// 设置数字,讲数字拆成Interger放入一个Vector中

if (num != null) {

this.num = new VectorInteger(10);

int beginIndex = 0;

int endIndex = -1;

Integer integer = null;

while ((endIndex = num.indexOf(SEP, beginIndex)) != -1) {

integer = Integer.valueOf(num.substring(beginIndex, endIndex));

if (integer == null || integer.intValue() = this.base

|| integer.intValue() 0) {

this.num = null;

throw new Exception("数据存在错误,请检查。");

}

this.num.addElement(integer);

beginIndex = endIndex + 1;

integer = null;

}

integer = Integer.valueOf(num.substring(beginIndex));

this.num.addElement(integer);

integer = null;

} else {

throw new Exception("数字为空");

}

}

/**

* 根据十进制数,初始化一个P进制数

*

* @param base

* 数制

* @param num

* 10进制数

* @throws Exception

*/

public void init(int base, int num) throws Exception {

setBase(base);

this.num = new VectorInteger(10);

Integer integer = null;

do {

integer = Integer.valueOf(num % this.base);

this.num.insertElementAt(integer, 0);

integer = null;

num /= this.base;

} while (num 0);

}

/**

* 将P进制数转换成10进制。 算法为Decimal

* =Square(Pn,n)+...+Square(P2,2)+Square(P1,1)+Square(P0,0)

*

* @return

* @throws Exception

*/

public int toDecimal() throws Exception {

if (this.base 2 || this.num == null || this.num.size() 0)

throw new Exception("没有数据");

int result = 0;

Integer interger = null;

int step = this.num.size() - 1;// 最高次幂

for (int i = 0; i this.num.size(); i++) {

interger = this.num.elementAt(i);

if (interger != null) {

result += interger.intValue() * square(this.base, step);

step--;

}

interger = null;

}

return result;

}

/**

* 设置数制

*

* @param base

* 数制

* @throws Exception

*/

public void setBase(int base) throws Exception {

if (base = 1)

throw new Exception("数制存在错误,必须大于1");

this.base = base;

}

public String toString() {

StringBuffer sb = new StringBuffer();

sb.append("数制 = ");

sb.append(this.base);

sb.append('\n');

sb.append("数字 = ");

for (int i = 0; i this.num.size(); i++) {

sb.append(((Integer) this.num.elementAt(i)).intValue());

sb.append(':');

}

sb.deleteCharAt(sb.length() - 1);

return sb.toString();

}

/**

* 计算平方,此处采用递归算法

*

* @param base

* 底数

* @param n

* 指数

* @return 平方结果

*/

private int square(int base, int n) {

int result = base;

if (n == 0) {

return 1;

} else {

result *= square(base, n - 1);

}

return result;

}

}

public class Test {

public static void main(String[] args) {

try {

P_NUM p1 = new P_NUM(6, "1:2:3:4:5");

P_NUM p2 = new P_NUM(6, "5:4:3:2:1");

System.out.println("p1.toDecimal() = " + p1.toDecimal());

System.out.println("p2.toDecimal() = " + p2.toDecimal());

int result = p1.toDecimal() * p2.toDecimal();

System.out.println("(十进制)p1 * p2 = " + result);

P_NUM p3 = new P_NUM(6, result);

System.out.println("(P进制)p1 * p2 = " + p3.toString());

} catch (Exception e) {

e.printStackTrace();

}

}

}

原理很简单。将P进制数转换成10进制数进行计算,再将结果转换成P进制数。代码中有注释,其中toDecimal()方法将P进制数转换成10进制数,init(int base, int num)将10进制数转成P进制数。

Java从键盘上输入两个数字求相乘,怎么写代码?

以下代码请lz存为Mutiply.java文件,然后直接编译运行即可。

-------------

import java.util.Scanner;

public class Mutiply {

public static void main(String[] args) {

int a=0,b=0;

Scanner scanner = new Scanner(System.in);

System.out.printf("请输入两个数,以空格区分" );

a = scanner.nextInt();

b= scanner.nextInt();

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

}

}

java编程实现任意两个数组的乘法运算

mport java.util.Scanner;

public class liujian {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

A num;

num=new A();

Scanner in = new Scanner(System.in);

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

num.a = in.nextInt();

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

num.b = in.nextInt();

System.out.println("两数相加的结果为:");

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

System.out.println("两数相减的结果为:");

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

System.out.println("两数相乘的结果为:");

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

System.out.println("两数相除的结果为:");

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

System.out.println("两数平方的结果为:");

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

}

}

class A{

int a,b;

void jjcc(int s){

System.out.println(s);

}

}

JAVA 两个数相乘怎么写?

public class Day25B {

public static void main(String[] args) {

baiint[] arr1=new int[5],arr2=new int[5],result=new int[5];

for (int i = 0; i result.length; i++) {

arr1[i]=(int)Math.round(Math.random()*40+10);

arr2[i]=(int)Math.round(Math.random()*40+10);

result[i]=arr1[i]*arr2[i];

System.out.println("索引\tarr1\tarr2\tresult");

for (int i = 0; i result.length; i++) {

System.out.println(i+"\t"+arr1[i]+"   x   "+arr2[i]+"   =\t"+result[i]);

扩展资料:

javap 类文件反汇编器数据类型boolean 布尔型

byte 字节型

char 字符型

short 短整型

int 整形

long 长整形

float 单精度浮点型

double 双精度浮点型class 类null 空类型

interface 接口


当前文章:java代码让两个数相乘,两个数相乘的代码
分享路径:http://6mz.cn/article/hsiigd.html

其他资讯