我目前正在从事一项课堂任务,涉及计算具有单个尺寸的任意数量的墙所需的油漆量,但事实是,我已经对其进行了设置,所有计算和所有工作均正确进行。
我正在使用IF语句将EU转移到正确的代码块,但是由于某些原因,当我使用英尺到英制加仑部分时,它仍然尝试将米转换为升。我显然在第一个IF语句和DO WHILE循环(用于计算墙壁总面积和房间过大面积的循环)的末尾缺少某些内容,我只是想知道是否有人可以快速看一下它,然后为我指明正确的方向。如果您有任何帮助,请附在下面的代码中。
(对于任何不符之处第一次道歉,请在这里大声笑)
public static void main(String[] args) throws IOException
{
double widthOfWall = 0;
double lengthOfWall = 0;
double areaOfWall = 0;
double areaOfRoom = 0;
double paintNeeded = 0;
char UnitsUsed = ' ';
char extraCalc = 0;
//Scanner class
Scanner keyboard = new Scanner (System.in);
{
System.out.println("Are using Feet or Metre? (F/M)");
UnitsUsed = ValidateData.checkTwoChars('F', 'M');
{
{
if (UnitsUsed == 'F')
do{
System.out.println("Please enter width of the wall");
widthOfWall = keyboard.nextDouble();
System.out.println("Please enter the Length of the wall");
lengthOfWall = keyboard.nextDouble();
areaOfWall = widthOfWall * lengthOfWall;
System.out.println("The area of this wall is " + areaOfWall);
System.out.println("Do you require an additional calculation? ");
extraCalc = ValidateData.checkTwoChars('Y', 'N');
areaOfRoom += areaOfWall;
areaOfWall ++;
System.out.println("The area of the room " + areaOfRoom);
paintNeeded = areaOfRoom / 6.229;
System.out.println("The amount of paint needed (in Imperial Gallons) " + paintNeeded);
}
while (extraCalc == 'Y');
else if (UnitsUsed == 'M');
//Do while loop
do{
System.out.println("Please enter width of the wall");
widthOfWall = keyboard.nextDouble();
System.out.println("Please enter the Length of the wall");
lengthOfWall = keyboard.nextDouble();
areaOfWall = widthOfWall * lengthOfWall;
System.out.println("The area of this wall is " + areaOfWall);
System.out.println("Do you require an additional calculation? ");
extraCalc = ValidateData.checkTwoChars('Y', 'N');
areaOfRoom += areaOfWall;
areaOfWall ++;
}
while (extraCalc == 'Y');
System.out.println("The area of the room " + areaOfRoom);
paintNeeded = areaOfRoom / 12;
System.out.println("The amount of paint needed (in litres) " + paintNeeded);
}
}
}}
}
您这里用分号代替左花括号{
:
else if (UnitsUsed == 'M');
同样,当两个分支之间唯一的区别是单位时,这就是很多重复的代码。您可以使用三元运算符设置乘数和单位字符串变量,然后消除if / else来简化此过程。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句