简体   繁体   中英

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.

       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:

  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.

        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

    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

Your for loop can only execute once because every switch case statement has a break statement at the end. As others have mentioned, you should try doing this without a for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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