java中获取错误,在简单程序中获取分段错误
我在學校放假期間一直在教自己C,并且最近嘗試編寫一個簡單的計算器程序,該程序應該采用兩個整數并對它們執行四個操作之一( - * /),但每當第一個變量被賦值時,我得到分段錯誤/核心轉儲錯誤消息 . 我知道這與內存分配有關,我嘗試過使用指針和malloc,但我懷疑我做錯了 .
#include
#include
#include
int calculate(int numberOne, int numberTwo, int operator);
int main(){
//Declaring Variables
int numberOne, numberTwo, total, operator;
int *one, *two, *tot, *op;
//Assigning Variables
printf("Integer 1: ");
scanf("%d", numberOne);
printf("\nOperator 1[+] 2[-] 3[*] 4[/] : ");
scanf("%d", operator);
printf("Integer 2: ");
scanf("\n%d", numberTwo);
//Output Calculatoin Through Function
printf("Calculation Complete: %d is the answer", calculate(numberOne, numberTwo, operator));
}
int calculate(int numberOne, int numberTwo, int operator) {
int total = 0;
do{
switch(operator){
case 1:
total = numberOne + numberTwo;
break;
case 2:
total = numberOne - numberTwo;
break;
case 3:
total = numberOne*numberTwo;
break;
case 4:
total = numberOne/numberTwo;
break;
default:
printf("Error, Invalid Operator, Please Enter A New One: ");
scanf("%d", operator);
}
}while(total ==0);
return total;
}
總結
以上是生活随笔為你收集整理的java中获取错误,在简单程序中获取分段错误的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java ubuntu 14.04,ub
- 下一篇: php多线程swoole,swoole究