开关循环?而循环?货币转换器程序java

时间:2015-01-07 23:32:38

标签: java

我是新手切换循环,我正试图创建这个货币转换器程序有多个问题。 首先,我想循环用户1继续输入值的情况1,直到他们输入-1,因此它停止并继续前进。目前,它没有这样做。一旦我在开关1上输入了GPR值,然后循环回到保持原始GPR存储值的菜单。

代码在这里:

import java.util.Scanner;
public class Conversion {

public static void main(String[] args) {
    double pound;
    double euro;
    double dollars;
    double yen;
    double rupees;
    double poundEuro;
    double poundDollars;
    double poundYen;
    double poundRupees;
    int Choice;


    Scanner input = new Scanner(System.in);
    Scanner exchange = new Scanner(System.in);

    System.out.println("Please choose an option:");
    System.out.println("1.  Enter values and type -1 to stop");
    System.out.println("2.  Euros (1GBP = 1.28EUR)");
    System.out.println("3.  Dollars (1GBP = 1.51USD)");
    System.out.println("4.  Yen (1GBP = 179.80JPY)");
    System.out.println("5.  Rupees (1GBP = 95.60INR)");
    System.out.println("6.  Exit");

    Choice = input.nextInt();


    switch (Choice) {

    case 1:

        while (!exchange.equals("-1"));{
            pound = exchange.nextDouble();  
            System.out.print("Please enter your values you would like to exchange (Type '-1' to stop) ");
        }
    case 2:
        pound = exchange.nextDouble();
        dollars = 1.51;
        poundDollars = pound * dollars; 
        System.out.println("Your amounts in euros are" + poundDollars);

    case 3:
        pound = exchange.nextDouble();
        yen = 1.28;
        poundYen = pound * yen; 
        System.out.println("Your amounts in euros are" + poundYen);

    case 4:
        pound = exchange.nextDouble();
        rupees = 1.28;
        poundRupees = pound * rupees; 
        System.out.println("Your amounts in euros are" + poundRupees);

    case 5:
        pound = exchange.nextDouble();
        euro = 1.28;
        poundEuro = pound * euro; 
        System.out.println("Your amounts in euros are" + poundEuro);

    case 6:
        break;
    }

    input.close();
    exchange.close();


}

}

3 个答案:

答案 0 :(得分:1)

switch不是循环。这是一个像if这样的分支声明。它有break,但这只是因为你可以“落到”下一个case语句。目前你大部分时间都在考虑,因为你忘了在案例之间插入break陈述。

答案 1 :(得分:0)

switch语句不是循环。将其视为一系列else-if语句的替代品。您的代码实际上读起来更像这样:

if(Choice == 1)
{

}
else if(Choice == 2)
{

}
else if(Choice == 3)
{

}
else if(Choice == 4)
{

}
else if(Choice == 5)
{

}
else if(Choice == 5)
{

}

但是,您的切换案例应以break语句终止。如果不是,那么与上面的else-ifs不同,执行将落到下一个案例并执行该代码中的代码,直到它最终到达break语句或经历所有情况。

你想要的是你的while循环包装你的switch语句AROUND,但你应该首先修复你的while循环,因为你用分号终止它,它是无限的。 while循环最后不需要分号,除非你没有它的主体,但你确实在条件检查中发生了一些副作用,最终导致它结束。

while( blah )
{
    switch( blah )
    {
        case 1:
            // Do stuff for this case
            break;
        case 2:
            // Do stuff for this case
            break;
        default:
            // Do stuff when no case is matched
            break;
    }
}

答案 2 :(得分:0)

我觉得你有点困惑。正如immibis在他的评论中指出的那样,switch语句不是循环,因此您需要将switch语句括在循环中。

然后,您在每个break的末尾错过了case语句。引用the Java tutorials

  

每个break语句都会终止封闭的switch语句。控制流继续switch块之后的第一个语句。 break语句是必要的,因为如果没有它们,switch中的语句将通过 :匹配case标签后的所有语句都是在遇到case语句之前,无论后续break标签的表达如何,都会按顺序执行。

你的程序应该是这样的:

public static void main(String[] args) {
    // Variable definitions
    while(true) {  // This is a little trick to force the application to repeat
                   // a task until you explicitly break the loop
        // Code to print your menu
        choice = input.nextInt();
        if(choice == -1 || choice == 6)
            break; // Break the while loop if choice is -1 or 6
        switch(choice){
            case 1:
                // Your code for option 1
                break;
            case 2:
                // Your code for option 2
                break;
            // More cases
            default:
                System.out.println("You must enter an option between 1 and 6!");
                break; // Not strictly needed
        }
    }
}

如果您不在每个break块的末尾添加case个语句,那么您的程序将落在每个选项中。

请阅读:


另一种选择是这样的:

public static void main(String[] args) {
    // Variable definitions
    /*
       Now let's use a labeled loop
     */
    menu: while(true) { 
        // Code to print your menu
        choice = input.nextInt();
        switch(choice){
            case -1:  // Nothing goes here, so the execution will fall through
                      // to the next case
            case 6:
                break menu; // This will break the menu loop.
                            // Since the previous case simply falls through to 
                            // this one, this will happen if choice is 
                            // either -1 or 6
            case 1:
                // Your code for option 1
                break;
            case 2:
                // Your code for option 2
                break;
            // More cases
            default:
                System.out.println("You must enter an option between 1 and 6!");
                break; // Not strictly needed
        }
    }
}

更多阅读: