[英]Java- a constructer that analyzes a string a converts it into an int
您如何創建具有 for 循環的構造函數,該循環遍歷或分析字符串。 然后將字符串字符轉換為整數,然后將其存儲到數組列表中。 例如,如果數字是“500”,它應該是 5 0 0,每個數字都是數組列表塊。
這是我到目前為止的代碼;
import java.util.Scanner;
import java.util.ArrayList;
public class BigInt {
private String BigInt;
private boolean pos;
private boolean neg;
private ArrayList<Integer> bigNum = new ArrayList<Integer>();
public BigInt(String b1){
b1 = "";
for(Integer num : bigNum){
}
//in a for loop if integer put into the array list
}
public boolean sign(boolean a, boolean b){
a = false;
b = true;
return false;
}
public String toString(){
//check if boolean is + or - set that as the first character in the string
//if( string - == first character)
return BigInt;
}
}
您可以做的一件事是使用 / 和 % 來分解大數。 像這樣的東西
while (a > 10) {
arrayList.insert(0, a % 10)
a /= 10;
}
也許像這樣?
int num, index;
num = Integer.parseInt(b1);
index = 0;
while (num >= 0)
{
arrayList.insert(index, num % 10);
num = num / 10;
index = index + 1;
}
如何創建遍歷或分析字符串的 for 循環。 然后將字符串字符轉換為整數
int x = Integer.valueOf(BigInt);
然后將其存儲到一個數組列表中
for (int i = 0; i < BigInt.length(); i++)
{
BigNum.add(BigInt.charAt(i) - '0');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.