int newIndex = (int) Math.random() * intList.size();
我有雇员,我想制作一个简单的程序,其中我使用switch语句来扣除拥有10,000R以上的不同员工的工资......但是编译器没有显示错误但是程序没有运行如图所示给出输出我很少混淆。
答案 0 :(得分:3)
您在switch
上添加了salary
,但没有给变量赋值。这导致salary
具有垃圾值。
只需将这些行放在switch
之外:
cout<<"enter salary amount :";
cin>>salary;
// now start the switch statement here:
switch(...)
{
....
}
这样,您首先提示用户输入salary
,然后再对其执行所需的操作。
答案 1 :(得分:2)
我在您的代码中看到3个错误。我纠正了你的代码并写了评论以突出它们。
请参阅以下内容:
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
// 1) Declare all variables of same type to avoid implicit casting errors.
// In this case we need float or double types.
float salary;
float deduction;
float netpayable;
// 2) This block must be out of switch instruction!
cout<<"enter salary amount :";
cin>>salary;
// 1.1) The switch will do the expected job
// only if it works on a int variable, so I put an explicit cast
// to (int).
switch((int)(salary/10000)){
case 0:
deduction=0;
netpayable = salary;
cout<<"netpayable salary is salary-deduction ="<<netpayable;
break;
case 1:
deduction=1000;
netpayable = salary-deduction;
cout<<"netpayable salary is salary-deduction ="<<netpayable;
break;
default:
// 3) (7/100) = 0 because compiler interprets it as integer.
// Use (7./100.) instead.
deduction=salary*(7./100.);
netpayable = salary-deduction;
cout<<"netpayable salary is salary-deduction ="<<netpayable;
break;
}
system("pause");
}