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

标签 java validation while-loop

我应该向货币转换器添加一个 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!");
      }     

    }

}

最佳答案

你只需要把输入和验证代码放在一个while循环中,并使用一个标志来控制是否循环回来。大致如下:

boolean invalidInput;
do {
    System.out.println("do you want to convert to Peso or Yen?");
    char type = k.next().charAt(0); //get 1st char of user input

    invalidInput = false;
    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");
      invalidInput = true;
    }
} while (invalidInput);

关于java货币转换器输入验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51251286/

相关文章:

java - 在 Java 中使用值为 1 的 boolean 值

java - 编译器错误 : Type mismatch when assigned interface variable to a class

java - 如何对满足过滤条件的单声道结果​​执行操作

java - Java中有没有办法在不尝试创建文件的情况下确定路径是否有效?

c++ - 简单 while 循环的问题

java - 如何在一定时间后退出 while 循环?

python - 为什么这个 python while 循环缺少逻辑运算符?

java - POST 大消息时 HttpUrlConnection#setReadTimeout 无效

css - validator.w3.org 和 jigsaw.w3.org css 验证器之间的区别?

ruby-on-rails - 自定义 Rails 验证抛出未定义的方法错误