猿问

Java货币转换器输入验证问题

我应该在货币转换器中添加一个while循环,并应该检查用户是否输入了除Y,y,P或p以外的其他字母,并提示他们再次尝试重新输入其货币类型。


我正在努力知道将其放置在我的代码中的位置。任何帮助将不胜感激。货币转换器的代码是:


import java.util.Scanner;


public class CurrencyConverter 

{


public static void main(String[] args) 

{


    //Store these 2 conversion rate as constant (final) variables

    final double PESO = 20.37;

    final double YEN = 114.37;


    double total =0;


    //Get the data from the user

    Scanner k = new Scanner(System.in);


    //Get the amount of USD

    System.out.println("how much money do you want to convert?");

    double usd = k.nextDouble();


    //Get the conversion type (peso or yen)

    System.out.println("do you want to convert to Peso or Yen?");

    char type = k.next().charAt(0); //get 1st char of user input


      switch(type)

      {

        case 'p':

        case 'P':

          //convert and print

          total = usd * PESO;

          System.out.printf("$%.2f = %.2f Peso\n", usd, total);

          break;

        case 'y':

        case 'Y':

          //convert and print

          total = usd * YEN;

          System.out.printf("$%.2f = %.2f Yen\n", usd, total);

          break;

        default:

          System.out.println("Sorry Invalid Currency type, " + 

                             "please try again");

          System.out.println("do you want to convert to Peso or Yen?");

          type = k.next().charAt(0);

      }


      if ((usd >= 1000) && (type=='p' || type=='P'))

      {

        System.out.println("You're going to have a blast in Mexico");

      }

      else if ((usd > 5000) && (type=='y' || type=='Y'))

      {

        System.out.println("Have a great time in Japan!");

      }

      else if (usd < 10)

      {

        System.out.println("Haha you're broke!");

      }     


    }


}



万千封印
浏览 165回答 3
3回答

拉莫斯之舞

您只需要将输入和验证代码括在while循环中,并使用一个标志来控制是否回送。遵循以下原则:boolean invalidInput;do {&nbsp; &nbsp; System.out.println("do you want to convert to Peso or Yen?");&nbsp; &nbsp; char type = k.next().charAt(0); //get 1st char of user input&nbsp; &nbsp; invalidInput = false;&nbsp; &nbsp; switch(type)&nbsp; &nbsp; {&nbsp; &nbsp; case 'p':&nbsp; &nbsp; case 'P':&nbsp; &nbsp; &nbsp; //convert and print&nbsp; &nbsp; &nbsp; total = usd * PESO;&nbsp; &nbsp; &nbsp; System.out.printf("$%.2f = %.2f Peso\n", usd, total);&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 'y':&nbsp; &nbsp; case 'Y':&nbsp; &nbsp; &nbsp; //convert and print&nbsp; &nbsp; &nbsp; total = usd * YEN;&nbsp; &nbsp; &nbsp; System.out.printf("$%.2f = %.2f Yen\n", usd, total);&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; System.out.println("Sorry Invalid Currency type, " +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"please try again");&nbsp; &nbsp; &nbsp; invalidInput = true;&nbsp; &nbsp; }} while (invalidInput);

人到中年有点甜

在切换之前将bool for loop竞争设置为falsebool selected = false;接下来,在开关周围创建一个while循环,设置为在“ selected”布尔值为false时循环&nbsp; while(!selected){&nbsp; &nbsp; &nbsp; switch(type)在成功选择用户后,将布尔值更改为true&nbsp; &nbsp; &nbsp; &nbsp; case 'p':&nbsp; &nbsp; &nbsp; &nbsp; case 'P':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selected = true;
随时随地看视频慕课网APP

相关分类

Java
我要回答