在Java中循环if语句

时间:2014-11-24 22:28:48

标签: java loops if-statement

嘿,我有一个if和else语句,如果发生其他情况我想循环,因为这意味着用户没有输入所需的数量,所以程序重新启动这里是代码的任何想法吗?

System.out.println("The area of the glass is " + area );

    if (thick == 3)
    {

        System.out.println("The price to replace the glass will be £" + price1);
        System.out.println("With VAT the price is £" + Final1  );
    }
    else if (thick == 5)
    {

        System.out.println("The price to replace the glass will be £" + price2);
        System.out.println("With VAT the price is £" + Final2);
    }

    else if (thick == 7)
    {

    System.out.println("The price to replace the glass will be £" + price3);
    System.out.println("With VAT the price is £" + Final3);
    }
    else
    {
    System.out.println("Sorry for the inconvenience but we do not do this size thinkness.");    //If the we don't have the thickness this is displayed 
    }

提前致谢

6 个答案:

答案 0 :(得分:2)

使用while循环循环,直到变量可以接受为止:

thick = 0;
while (thick != 3 && thick != 5 && thick != 7) {
    // read new thick value from user
    // your current code
}

您可以更加整洁地表达以上内容:

for (thick = 0; !Arrays.asList(3, 5, 7).contains(thick);)
    // read new thick value from user
    // your current code
}

答案 1 :(得分:0)

你可以使用一个循环。如果输入了有效的响应,请退出循环。否则,请继续循环。

示例:

while(true){
    <get input here>
    if(thick==3){
        <do stuff>
        break;
    }
    else if(...){
        <do stuff>
        break;
    }
    ...
    else{
        <do stuff>
    }
}

答案 2 :(得分:0)

do {
your code 
}while(the_else_occurs);

即:

boolean flag=false;
do{
    if (thick == 3)
    {

        System.out.println("The price to replace the glass will be £" + price1);
        System.out.println("With VAT the price is £" + Final1  );
    }
    else if (thick == 5)
    {

        System.out.println("The price to replace the glass will be £" + price2);
        System.out.println("With VAT the price is £" + Final2);
    }

    else if (thick == 7)
    {

    System.out.println("The price to replace the glass will be £" + price3);
    System.out.println("With VAT the price is £" + Final3);
    }
    else {
        flag=true;
    System.out.println("Sorry for the inconvenience but we do not do this size thinkness.");    //If the we don't have the thickness this is displayed
    }
    }while(flag);

答案 3 :(得分:0)

我认为你应该投入一些输入验证:

while(thick != 3 && thick != 5 && thick != 7 && thick != null){

    System.out.println("Sorry for the inconvenience but we do not do this size thinkness.");    

    //If the we don't have the thickness this is displayed 
}

答案 4 :(得分:0)

如果没有正确输入,你也可以尝试创建一个方法并调用它(递归):

public void MyMethod() {

System.out.println("The area of the glass is " + area );

if (thick == 3)
{

    System.out.println("The price to replace the glass will be £" + price1);
    System.out.println("With VAT the price is £" + Final1  );
}
else if (thick == 5)
{

    System.out.println("The price to replace the glass will be £" + price2);
    System.out.println("With VAT the price is £" + Final2);
}

else if (thick == 7)
{

System.out.println("The price to replace the glass will be £" + price3);
System.out.println("With VAT the price is £" + Final3);
}
else
{
System.out.println("Sorry for the inconvenience but we do not do this size thinkness.");    //If the we don't have the thickness this is displayed 
MyMethod();
}
}

唯一的问题是你必须小心堆栈溢出。尽管如此,switch语句可能是一个好主意。只是一些建议。

答案 5 :(得分:0)

您可以改为使用哈希映射:

HashMap<int, PointF> map = new HashMap<int, PointF>()
{
    { 3, new PointF(4.5f, 6) },
    { 5, new PointF(8, 6.3f) },
    { 6, new PointF(9, 8) },
};

if (map.containsKey(thick))
{
   System.out.println("The price to replace the glass will be £" + map.get(thick).x);
   System.out.println("With VAT the price is £" + map.get(thick).y);
}

如果您可以使用自定义类切换PointF,那么它会更好