Java 防止incrementor关闭带有有效输入的程序

Java 防止incrementor关闭带有有效输入的程序,java,validation,increment,Java,Validation,Increment,我制作了一个可编译并运行的货币转换器,但其目的之一是允许最多3次尝试输入要转换的正确货币类型。第三次尝试时遇到的问题是,无论用户输入是否正确(字符“Y”、“Y”、“P”或“P”),程序仍然会关闭。如何修复此问题,使其仅在输入错误字符时在第三次尝试时关闭 public class CurrencyConverter { public static void main(String[] args) throws IOException { //Store these 2 con

我制作了一个可编译并运行的货币转换器,但其目的之一是允许最多3次尝试输入要转换的正确货币类型。第三次尝试时遇到的问题是,无论用户输入是否正确(字符“Y”、“Y”、“P”或“P”),程序仍然会关闭。如何修复此问题,使其仅在输入错误字符时在第三次尝试时关闭

public class CurrencyConverter 
{

  public static void main(String[] args) throws IOException
  {

    //Store these 2 conversion rate as constant (final) variables
    final double PESO = 20.37, YEN = 114.37;
    //initialize variable for the converted currency
    double total = 0;

    char type; //currency type
    char decision = 'Y'; //variable for user decision to re-run program
    int attempt = 1; //initializing attempts variable to increment attempts

    //Get the data from the user
    Scanner k = new Scanner(System.in);

    do
    {
      //boolean statement for while loop
      boolean input = false;
      //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?");
      type = k.next().charAt(0); //get first character of whatever user types

      while((!input) && (attempt != 3))
      {
        switch(type)
        {
          case 'p':
          case 'P':
            input = true;
            //convert and print
            total = usd * PESO;
            System.out.printf("$%.2f = %.2f Peso\n", usd, total);
            break;
          case 'y':
          case 'Y':
            input = true;
            //convert and print
            total = usd * YEN;
            //System.out.printf("$" + usd + " = " + total + " Yen"); no formatting
            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);
            //increment attempt
            attempt++;
            //close program after 3 failed attempts
            if(attempt == 3)
            {
              System.out.print("Too many failed attempts, goodbye!");
              System.exit(1);
            }
        }
        //if-else-if statement
        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!");
        }     
      }
      System.out.print("Would you like to re-run the program? (Y=yes, N=no)");
      decision = k.next().charAt(0);
    }while((decision == 'Y') || (decision == 'y'));

    FileWriter fw = new FileWriter("output.txt", true);
    PrintWriter pw = new PrintWriter(fw);     
    pw.println(total);
    pw.println(type);
    pw.close();

    k.close();
  }

}
公共类CurrencyConverter
{
公共静态void main(字符串[]args)引发IOException
{
//将这2个转换率存储为常量(最终)变量
最终双倍比索=20.37,日元=114.37;
//初始化已转换货币的变量
双倍合计=0;
字符类型;//货币类型
char decision='Y';//用于用户决定重新运行程序的变量
int-trunt=1;//初始化尝试变量以增加尝试次数
//从用户处获取数据
扫描仪k=新的扫描仪(System.in);
做
{
//while循环的布尔语句
布尔输入=假;
//得到美元的金额
System.out.println(“您想兑换多少钱?”);
美元加倍=k.下一倍();
//获取转换类型(比索或日元)
System.out.println(“您想转换成比索还是日元?”);
type=k.next().charAt(0);//获取任何用户类型的第一个字符
而((!input)&(trust!=3))
{
开关(类型)
{
案例“p”:
案例“P”:
输入=真;
//转换和打印
总额=美元*比索;
System.out.printf(“$%.2f=%.2f比索”,美元,总计);
打破
案例“y”:
案例“Y”:
输入=真;
//转换和打印
总额=美元*日元;
//System.out.printf(“$”+usd+“=”+total+“日元”);无格式
System.out.printf(“$%.2f=%.2f日元”,美元,总计);
打破
违约:
System.out.println(“对不起,货币类型无效,请重试”);
System.out.println(“您想转换成比索还是日元?”);
type=k.next().charAt(0);
//增量尝试
尝试++;
//尝试3次失败后关闭程序
如果(尝试==3)
{
System.out.print(“尝试失败太多,再见!”);
系统出口(1);
}
}
//if-else-if语句
如果((美元>=1000)和&(类型=='p'| |类型=='p'))
{
System.out.println(“你将在墨西哥经历一场爆炸”);
}
如果((美元>5000美元)和&(类型='y'| |类型='y'))
{
System.out.println(“祝您在日本玩得愉快!”);
}
如有其他情况(美元<10美元)
{
System.out.println(“哈哈,你破产了!”);
}     
}
System.out.print(“是否要重新运行程序?(Y=yes,N=no)”;
decision=k.next().charAt(0);
}而((decision='Y')| |(decision='Y'));
FileWriter fw=新的FileWriter(“output.txt”,true);
PrintWriter pw=新的PrintWriter(fw);
pw.println(总计);
pw.println(类型);
关闭();
k、 close();
}
}

在if尝试中,您可以添加验证,确认if与“Y”、“Y”、“p”或“p”不同,将继续退出程序

 if(attempt == 3 && type != 'y' && type!='Y' && type!='p' && type!='P')
                        {
                            System.out.print("Too many failed attempts, goodbye!");
                            System.exit(1);
                        }