我有一個關於我有任務的快速問題。我想製作一種計算器,它將讀取用戶的輸入,使用if-else-loop找出如何處理它,然後打印相應的結果。 因此,如果用戶輸入:4 * 5,我希望程序檢查兩個整數,並用inChar檢查所需計算的類型,然後執行if-else-loop,其餘部分將完成(我知道如何寫這部分),但是我需要一些關於如何編寫掃描器句子的幫助,它可以檢查一行中不同類型的輸入。希望你明白我的意思。如何讓程序在java中接受來自終端的不同輸入類型的一行?
代碼的一部分,我已經有了:
Scanner input = new Scanner(System.in);
int a,b;
char c;
double sum;
System.out.println("Velkommen til en helt enkel kalkulator!");
System.out.println("Vennligst tast inn regnestykket, paa formen: tall, regneart, tall med mellomrom imellom hver input: ");
String svar=input.nextLine();
if(c='*'){
sum=a*b;
}else if(c='/'){
sum=a/b;
}else if(c='+'){
sum=a+b;
}else if(c='-'){
sum=a-b;
}else{
System.out.println("Regnearten var ikke forstaaelig. Programmet termineres.");
}
秀部分代碼與掃描儀,你有.. –
簡短和精確的問題獲取更多的關注,只是說。 –