導航:首頁 > 編程語言 > javastring計算

javastring計算

發布時間:2022-05-26 01:41:03

A. java運算 String y="(1+2)*3"; 怎麼真接運算

首先你定義的是一個字元串,不是對應的數字運算,
然後
"(1+2)*3"
JAVA會直接輸出這個表達式,不會進行表達式的計算
如果想要進行計算,那麼需要更改如下:
int m = (1+2)*3;
String y=m + "";
這樣才會先進行數字的計算,然後轉成字元串

B. java怎麼進行string的運算

如果樓主問的是一個String中存的是數字怎麼進行計算的話:
String s = "11";
int a = Integer.parseInt(s);
那麼得到的a=11

C. java中的String型怎麼實現數學的加減乘除運算

public static void main(String[] args) {
// TODO Auto-generated method stub
BigInteger aa =new BigInteger("100");
BigInteger bb= new BigInteger("25");
BigInteger sub=aa.subtract(bb);//大整數的減
BigInteger add=aa.add(bb);//大整數的加
BigInteger mul=aa.multiply(bb);//大整數的乘
BigInteger div=aa.divide(bb);//大整數的除
System.out.println(sub.toString());
System.out.println(add.toString());
System.out.println(mul.toString());
System.out.println(div.toString());
}

D. java:把String類型裡面的表達式計算出來的 一個方法是什麼

沒有的,你定義的str是字元竄變數。那麼它定義出來的就是字元竄咯,沒有計算結果的。
要麼你就用
int i;
i=3+8*5-654*987;
System.out.println(i);

E. java計算出字元串中所有的數字求和

//提取字元串中所有數字
public static void main(String []args)
{
String str = "我們都是2007年3月28日參加課題設計的,對於我們來說也是第1次挑戰,但結果是好的,我們做到了100%滿意。";

char []ch = str.toCharArray();

//將字元串中的所有非數字替換成空格
for(int i=0;i<ch.length;i++)
{
if(!Character.isDigit(ch[i]))
{
ch[i]=' ';
}
}

//使用變換後的字元數組構建字元串,該字元串除了數字之外
//就是空格了,之後使用split分割過濾出空格就是字元串中
//的數字了。。。。
String str1 = new String(ch);
String []num = str1.trim().split(" ");
int sum = 0;
for(String s:num)
{
if(!"".equals(s.trim()))
{
sum+=Integer.ValueOf(s.trim());
}
}
System.out.println(sum);
}

F. 簡單的JAVA字元串長度計算的實現

簡單實現代碼如下:
public
class
stringlength
{
/**
*
獲取字元串的長度,如果有中文,則每個中文字元計為2位
*
@param
value
指定的字元串
*
@return
字元串的長度
*/
public
static
int
length(string
value)
{
int
valuelength
=
0;
string
chinese
=
"[\u0391-\uffe5]";
/*
獲取欄位值的長度,如果含中文字元,則每個中文字元長度為2,否則為1
*/
for
(int
i
=
0;
i
<
value.length();
i++)
{
/*
獲取一個字元
*/
string
temp
=
value.substring(i,
i
+
1);
/*
判斷是否為中文字元
*/
if
(temp.matches(chinese))
{
/*
中文字元長度為2
*/
valuelength
+=
2;
}
else
{
/*
其他字元長度為1
*/
valuelength
+=
1;
}
}
return
valuelength;
}
public
static
void
main(string
args[]){
string
str
=
"hello你好";
system.out.println(stringlength.length(str));
}
}

G. java字元串長度怎麼算

通過string自帶的length()方法獲取字元串長度。
string a="abcdefg";//定義一個字元串
int len = a.length();//通過length獲取字元串長度,這里等於7
length()該方法返回此字元串的長度。長度是等於unicode代碼單元中的字元串的數目。

H. java怎樣計算一個String類型的數學表達式

只有自己實現一個方法了,先將這類表達式轉換成逆波蘭式表達式,再使用棧進行計算,實現起來是有一定難度的,特別是其中還夾雜著冪、乘、除、括弧等有先後運算順序的運算符。

I. Java怎麼實現輸入一個string表達式然後輸出計算的結果

import java.io.*;
import java.util.*;
class BinaryTree{
BinaryTree left=null;
BinaryTree right=null;
char data=0;
}
public class Calculator{
String porder="";
public void postorder(BinaryTree bt){
//遞歸後序遍歷二叉樹
if(bt!=null){
postorder(bt.left);
postorder(bt.right);
porder+=bt.data;
}
}
public int calc(String s){
//計算後綴表達式的值
int i=0,len=0,a=0,b=0;
Stack stack=new Stack();
len=s.length();
for(;i<len;i++){
char c=s.charAt(i);
switch(c){
case '+':
a=(int)stack.pop();
b=(int)stack.pop();
stack.push(a+b);
break;
case '-':
b=(int)stack.pop();
a=(int)stack.pop();
stack.push(a-b);
break;
case '*':
a=(int)stack.pop();
b=(int)stack.pop();
stack.push(a*b);
break;
case '/':
b=(int)stack.pop();
a=(int)stack.pop();
stack.push((int)a/b);
break;
default:
//該字元是數字
stack.push(c-'0');
}
}
return stack.pop();
}
public BinaryTree create(String s){
//構造二叉樹結點(遞歸)
int i=0,pos=0,len=0;
String l="",r="";
BinaryTree node=new BinaryTree();
len=s.length();
if(len==0) return null;
if(len==1){
//遞歸邊界
node.data=s.charAt(0);
return node;
}
//去括弧
while((pos=s.indexOf(")"))!=-1){
i=pos;
while((s.charAt(i)!='(')&&(i>0)) i--;
String sub=s.substring(i+1,pos);//括弧中的表達式
porder="";
postorder(create(sub));//得到後綴表達式
int sum=calc(porder);//計算後綴表達式的值,替換原來括弧中的表達式
s=s.substring(0,i)+String.valueOf(sum)+s.substring(pos+1);
len=s.length();//重新計算s的長度
}
//從後往前掃描得到的第一個優先順序最低的運算符號的位置
pos=-1;
for(i=len-1;i>0;i--){
char c=s.charAt(i);
if((c=='+')||(c=='-')){
pos=i;
break;
}
else if(((c=='*')||(c=='/'))&&(pos==-1)) pos=i;
}
//在pos位置將s分為左右兩部分,遞歸構造左右兩個部分的二叉樹
l=s.substring(0,pos);
r=s.substring(pos+1);
node.data=s.charAt(pos);
node.left=create(l);
node.right=create(r);
return node;
}
public static void main(String args[]) throws Exception{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String s=reader.readLine();
Calculator c=new Calculator();
BinaryTree bt=c.create(s);
c.postorder(bt);
System.out.println(c.calc(c.porder));
}
}

閱讀全文

與javastring計算相關的資料

熱點內容
南京中興招收專科程序員嗎 瀏覽:297
代理商php源碼 瀏覽:983
蘋果手機怎麼解壓軟體app 瀏覽:650
游戲資源被編譯 瀏覽:152
代碼編譯後黑屏 瀏覽:8
程序員情侶寫真 瀏覽:505
python3孿生素數 瀏覽:36
計算楊輝三角Python 瀏覽:404
linux目錄重命名 瀏覽:196
演算法設計的最終形態是代碼 瀏覽:262
程序員社團招新橫幅 瀏覽:238
拖鞋解壓視頻大全 瀏覽:887
租伺服器主機鏈接軟體叫什麼 瀏覽:856
交叉編譯工具的linux版本號 瀏覽:156
python開發應用軟體 瀏覽:32
hdl綜合器與c編譯器的區別 瀏覽:899
編譯原理最左推導代碼 瀏覽:702
加密三 瀏覽:131
通過編譯鏈接後形成的可執行程序 瀏覽:680
怎麼用matlab編程 瀏覽:781