簡體   English   中英

Java貨幣轉換器輸入驗證問題

[英]java currency converter input validation problems

我應該在貨幣轉換器中添加一個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);

您可以只繞開switch ,然后用breakcontinue控制它

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();
    char type = ' ';

    while(true) {
        //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 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");
            continue;
        }
        break;
    }

    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!");
    }

}

在切換之前,將bool for loop競爭設置為false

bool selected = false;

接下來,在開關周圍創建一個while循環,設置為在“ selected”布爾值為false時循環

  while(!selected){
      switch(type)

成功選擇用戶后,將布爾值更改為true

        case 'p':
        case 'P':

            selected = true;

試試下面的代碼,它非常簡單:

import java.util.Scanner;

public class HelloWorld 
{

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

    while( true  ) {
      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);
          continue;
      }
      break;
    }

      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循環和布爾“ true”(一個continue和break語句)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM