简体   繁体   English

货币转换器-为什么我的for循环开关无法正常工作? 当我运行我的代码时,只有第一个for循环有效

[英]currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

`I have to do a code which user will input dollar amount and currency(which where the dollar will be converted) for example 15 YEN which 15 is the dollar amount and YEN is where to be converted.My code only runs in the first for loop which it will scan a string and split it but on second for loop which the conversion happens wont work. `我必须编写一个代码,用户将输入美元金额和货币(将在其中转换美元),例如15日元,其中15表示美元金额,而YEN是要转换的位置。我的代码仅在第一个中运行循环将扫描一个字符串并将其拆分,但是在第二个for循环中转换将无法进行。

       for(i=0;i<=3;i++){
       temp = sc.nextLine();
       Temp = temp.split(" ");
    }

       for(i=0,j=1;i<=3;i+=2,j+=2){
           switch (Temp[j]) {
               case "PHP":
                   conversion = Double.parseDouble(Temp[i])*51.23;
                   System.out.println("$"+Temp[0]+" CONVERTS TO "+df.format(conversion)+" PHP.");
                   break;
               case "POUNDS":
                   conversion = Double.parseDouble(Temp[i])*0.84;
                   System.out.println("$"+Temp[2]+" CONVERTS TO "+df.format(conversion)+" POUNDS.");
                   break;
               case "LIRA":
                   conversion = Double.parseDouble(Temp[i])*2040;
                   System.out.println("$"+Temp[4]+" CONVERTS TO "+df.format(conversion)+" LIRA.");
                   break;
               case "FRANCS":
                   conversion = Double.parseDouble(Temp[i])*9.85;
                   System.out.println("$"+Temp[6]+" CONVERTS TO "+df.format(conversion)+" FRANCS.");
                   break;
               case "MARKS":
                   conversion = Double.parseDouble(Temp[i])*3.23;
                   System.out.println("$"+Temp[8]+" CONVERTS TO "+df.format(conversion)+" MARKS.");
                   break;
               case "YEN":
                   conversion = Double.parseDouble(Temp[i])*260;
                   System.out.println("$"+Temp[10]+" CONVERTS TO "+df.format(conversion)+" YEN.");
                   break;
               default:
                   ;
                   break;
           }
        System.out.println("Invalid input, Please try again");
       }

You can omit the second loop and include the switch statement inside the first loop: 您可以省略第二个循环,并在第一个循环中包含switch语句:

  for(i=0;i<=3;i++)
  {
   temp = sc.nextLine();
   Temp = temp.split(" ");

       switch (Temp[1]) {
           case "PHP":
               conversion = Double.parseDouble(Temp[0])*51.23;
               System.out.println("$"+Temp[0]+" CONVERTS TO "+df.format(conversion)+" PHP.");
               break;
           // Other cases
           // ...
           // ...
           default:
               i--;
               System.out.println("Invalid input, Please try again");
       }
  }

and if you want to read all the 4 inputs (1 input per line) first before conversion then you need to store them in ArrayList of String s then pass them to the loop. 如果要在转换之前先读取所有4输入(每行1个输入),则需要将它们存储在StringArrayList ,然后将它们传递给循环。

        ArrayList<String> lines = new ArrayList<>();

        for(int i=0;i<=3;i++)
            lines.add(sc.nextLine());

        for(int i=0;i<=3;i++)
        {
            String Temp[] = lines.get(i).split(" ");

            switch (Temp[1]) {
                case "PHP":
                    conversion = Double.parseDouble(Temp[0])*51.23;
                    System.out.println(Temp[1]);
                    System.out.println("$"+Temp[0]+" CONVERTS TO "+df.format(conversion)+" PHP.");
                    break;
                // Other cases
                // ...
                // ...
                default:
                    continue;
                    //i--;
                    //System.out.println("Invalid input, Please try again");
            }

You don't need two loops if your input is one line, then you only need one scan 如果您的输入是一行,则不需要两个循环,则只需扫描一次

The second loop likely doesn't work because you repeat i<=3, meanwhile it seems your input is longer than 4 items .It'd be better to use the length of the split string, and you only need one variable to index it 第二个循环可能不起作用,因为您重复i <= 3,同时似乎您输入的内容多于4个项目。最好使用分割字符串的长度,并且只需要一个变量对其进行索引

    String temp = sc.nextLine().split(" ");
    double conversion;
    for (int i = 0; i < temp.length - 1; i +=2) {
        String amount = Double.parseDouble(temp[i]);
        String currency = temp[i+1];
        System.out.print("$"+amount+" CONVERTS TO ");

        switch (currency) {
            case "POUNDS":
               conversion = amount * (14.28/17);  // based on your comment, this isn't 51.23
               break;
        } 

        System.out.print(conversion + " " + currency);
        System.out.println();
    }

For an optimizaion, it'd be better if you used a Hashmap<String, Double> for the conversion rates, rather than a switch 为了优化,最好使用Hashmap<String, Double>作为转换率,而不是使用开关

Your for loop can only execute once because every switch case statement has a break statement at the end. 您的for循环只能执行一次,因为每个switch case语句的末尾都有一个break语句。 As others have mentioned, you should try doing this without a for loop. 正如其他人提到的那样,您应该尝试在没有for循环的情况下执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM