急!java菜鸟求救 if else 问题

请各位高手看以下代码。请问如何把if中的变量用在下一个if循环中呢?publicclassQ2{//-----------------------------------... 请各位高手看以下代码。
请问如何把if 中的变量用在下一个if循环中呢?
public class Q2
{ //---------------------------------------------------------------
//This program is to determine the minimum sales to reach a salary of $30,000 with commissions and a base salary $5000.
//---------------------------------------------------------------
public static void main(String[] args)
{
System.out.println("The base salary is ");
int baseSalary = Keyboard.readInt();
System.out.println("The goal Earn is");
int goalEarn = Keyboard.readInt();
int minimumCommission=goalEarn-baseSalary;

if (minimumCommission==0 && minimumCommission<5000)
{double commissionRate1=0.08;
double minimumSales1=minimumCommission/commissionRate1;
System.out.println("The minimum sales ammount is $" + minimumSales1);
}
else if (minimumCommission>=5000 && minimumCommission<10000)
{double commissionRate2=0.1;
double minimumSales2=(minimumCommission-5000)/commissionRate2 + minimumSales1;
System.out.println("The minimum sales ammount is $" + minimumSales2);
}
else if (minimumCommission>=10000)
{double commissionRate3=0.12;
double minimumSales3=(minimumCommission-10000)/commissionRate3 + minimumSales2;
System.out.println("The minimum sales ammount is $" + minimumSales3);
}

}
}
展开
 我来答
Christ_earl
2009-10-21 · TA获得超过1245个赞
知道小有建树答主
回答量:239
采纳率:0%
帮助的人:0
展开全部
把if中的变量声明在main()方法外,换句话说就是声明成全局变量 。但要注意的是,main()是静态的,所以成员变量也要是静态的; 例如:
public class Q2
{ //---------------------------------------------------------------
//This program is to determine the minimum sales to reach a salary of $30,000 with commissions and a base salary $5000.
//---------------------------------------------------------------

static double commissionRate1 ;
static double minimumSales1 ;
static double commissionRate2 ;
static double minimumSales2 ;
static double commissionRate3;
static double minimumSales3 ;
public static void main(String[] args)
{
System.out.println("The base salary is ");
int baseSalary = Keyboard.readInt();
System.out.println("The goal Earn is");
int goalEarn = Keyboard.readInt();
int minimumCommission=goalEarn-baseSalary;

if (minimumCommission==0 && minimumCommission<5000)
{ commissionRate1=0.08;
minimumSales1=minimumCommission/commissionRate1;
System.out.println("The minimum sales ammount is $" + minimumSales1);
}
else if (minimumCommission>=5000 && minimumCommission<10000)
{ commissionRate2=0.1;
minimumSales2=(minimumCommission-5000)/commissionRate2 + minimumSales1;
System.out.println("The minimum sales ammount is $" + minimumSales2);
}
else if (minimumCommission>=10000)
{ commissionRate3=0.12;
minimumSales3=(minimumCommission-10000)/commissionRate3 + minimumSales2;
System.out.println("The minimum sales ammount is $" + minimumSales3);
}
}
}
dulang2132
2009-10-21
知道答主
回答量:19
采纳率:0%
帮助的人:0
展开全部
把变量定义在if语句外面就可以
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式