急!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);
}
}
} 展开
请问如何把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);
}
}
} 展开
2个回答
展开全部
把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);
}
}
}
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);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询