新聞中心
怎么用Java把輸入的兩串字符串中的數(shù)字提取出來(lái),并且將兩串?dāng)?shù)字相乘輸出?
解決方案:
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的開(kāi)原網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
使用正則表達(dá)式抽取數(shù)字子串;
使用Interger.parse將數(shù)字子串轉(zhuǎn)為整數(shù)類型;
計(jì)算兩個(gè)數(shù)字相乘即可;
java編程,兩個(gè)p進(jìn)制數(shù)相乘
import java.util.Vector;
/**
* P進(jìn)制數(shù)
*
* @author Jarod Yv
*/
public final class P_NUM {
/** 數(shù)制 */
public int base = 0;
/** 數(shù)字。以Integer的形式保存在向量中 */
public VectorInteger num = null;
/** 數(shù)字間的分割符 */
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);
}
/**
* 初始化一個(gè)P進(jìn)制數(shù)
*
* @param base
* 數(shù)制 必須為2的整數(shù)
* @param num
* 數(shù)字 這里規(guī)定每一位之間用:號(hào)分割
* @throws Exception
*/
public void init(int base, String num) throws Exception {
// 設(shè)置數(shù)制
setBase(base);
// 設(shè)置數(shù)字,講數(shù)字拆成Interger放入一個(gè)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("數(shù)據(jù)存在錯(cuò)誤,請(qǐng)檢查。");
}
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("數(shù)字為空");
}
}
/**
* 根據(jù)十進(jìn)制數(shù),初始化一個(gè)P進(jìn)制數(shù)
*
* @param base
* 數(shù)制
* @param num
* 10進(jìn)制數(shù)
* @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進(jìn)制數(shù)轉(zhuǎn)換成10進(jìn)制。 算法為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("沒(méi)有數(shù)據(jù)");
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;
}
/**
* 設(shè)置數(shù)制
*
* @param base
* 數(shù)制
* @throws Exception
*/
public void setBase(int base) throws Exception {
if (base = 1)
throw new Exception("數(shù)制存在錯(cuò)誤,必須大于1");
this.base = base;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("數(shù)制 = ");
sb.append(this.base);
sb.append('\n');
sb.append("數(shù)字 = ");
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();
}
/**
* 計(jì)算平方,此處采用遞歸算法
*
* @param base
* 底數(shù)
* @param n
* 指數(shù)
* @return 平方結(jié)果
*/
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("(十進(jìn)制)p1 * p2 = " + result);
P_NUM p3 = new P_NUM(6, result);
System.out.println("(P進(jìn)制)p1 * p2 = " + p3.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
原理很簡(jiǎn)單。將P進(jìn)制數(shù)轉(zhuǎn)換成10進(jìn)制數(shù)進(jìn)行計(jì)算,再將結(jié)果轉(zhuǎn)換成P進(jìn)制數(shù)。代碼中有注釋,其中toDecimal()方法將P進(jìn)制數(shù)轉(zhuǎn)換成10進(jìn)制數(shù),init(int base, int num)將10進(jìn)制數(shù)轉(zhuǎn)成P進(jìn)制數(shù)。
Java從鍵盤上輸入兩個(gè)數(shù)字求相乘,怎么寫代碼?
以下代碼請(qǐng)lz存為Mutiply.java文件,然后直接編譯運(yùn)行即可。
-------------
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("請(qǐng)輸入兩個(gè)數(shù),以空格區(qū)分" );
a = scanner.nextInt();
b= scanner.nextInt();
System.out.println(a+"*"+b+"="+(a*b));
}
}
java編程實(shí)現(xiàn)任意兩個(gè)數(shù)組的乘法運(yùn)算
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("輸入第一個(gè)數(shù):");
num.a = in.nextInt();
System.out.print("輸入第二個(gè)數(shù):");
num.b = in.nextInt();
System.out.println("兩數(shù)相加的結(jié)果為:");
System.out.println(num.a+num.b);
System.out.println("兩數(shù)相減的結(jié)果為:");
System.out.println(num.a-num.b);
System.out.println("兩數(shù)相乘的結(jié)果為:");
System.out.println(num.a*num.b);
System.out.println("兩數(shù)相除的結(jié)果為:");
System.out.println(num.a/num.b);
System.out.println("兩數(shù)平方的結(jié)果為:");
System.out.println(num.a*num.b);
}
}
class A{
int a,b;
void jjcc(int s){
System.out.println(s);
}
}
JAVA 兩個(gè)數(shù)相乘怎么寫?
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]);
}
}
}
擴(kuò)展資料:
javap 類文件反匯編器數(shù)據(jù)類型boolean 布爾型
byte 字節(jié)型
char 字符型
short 短整型
int 整形
long 長(zhǎng)整形
float 單精度浮點(diǎn)型
double 雙精度浮點(diǎn)型class 類null 空類型
interface 接口
新聞標(biāo)題:java代碼讓兩個(gè)數(shù)相乘,兩個(gè)數(shù)相乘的代碼
新聞來(lái)源:http://fisionsoft.com.cn/article/hsiigd.html