我寫了一個PROGRAMM得到了許多的交叉總和:分割成數幾個號碼
所以,當我在3457例如鍵入它應該輸出3 + 4 + 5 + 7,但不知何故,我LOGIK不會工作。當我輸入68768例如我得到6 + 0 + 7.但是,當我輸入97999我得到正確的輸出9 + 7 + 9.我知道我可以用不同的方法輕鬆完成這項任務,但我試圖使用循環。這裏是我的代碼:並感謝所有
import Prog1Tools.IOTools;
public class Aufgabe {
public static void main(String[] args){
System.out.print("Please type in a number: ");
int zahl = IOTools.readInteger();
int ten_thousand = 0;
int thousand = 0;
int hundret = 0;
for(int i = 0; i < 10; i++){
if((zahl/10000) == i){
ten_thousand = i;
zahl = zahl - (ten_thousand * 10000);
}
for(int f = 0; f < 10; f++){
if((zahl/1000) == f){
thousand = f;
zahl = zahl - (thousand * 1000);
}
for(int z = 0; z < 10; z++){
if((zahl/100) == z){
hundret = z;
}
}
}
}
System.out.println(ten_thousand + " + " + thousand + " + " + hundret);
}
}
'9999'應該輸出什麼?你想忽略重複的數字嗎? –
@JohnSmith爲什麼不把它轉換爲字符串並遍歷字符串的每個字符? – Apostolos
你怎麼表達交叉?不明白爲什麼97999我得到正確的輸出9 + 7 + 9是正確的。是不同數字的總和? –