[英]java currency converter input validation problems
I'm supposed to add a while loop to a currency converter, and it's supposed to check if the user enters any letter other than Y, y, P, or p, and prompt them to try again re-enter their currency type. 我应该在货币转换器中添加一个while循环,并应该检查用户是否输入了除Y,y,P或p以外的其他字母,并提示他们再次尝试重新输入其货币类型。
I'm struggling to know where to place it in my code. 我正在努力知道将其放置在我的代码中的位置。 Any help would be greatly appreciated.
任何帮助将不胜感激。 The code for the currency converter is:
货币转换器的代码为:
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!");
}
}
}
You just need to enclose the input and validation code in a while loop, and use a flag to control whether to loop back. 您只需要将输入和验证代码括在while循环中,并使用一个标志来控制是否循环返回。 Something along these lines:
遵循以下原则:
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);
You can just wrap around the switch
, then control it with break
or continue
您可以只绕开
switch
,然后用break
或continue
控制它
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!");
}
}
Create a bool for loop competetion set to false before the switch 在切换之前,将bool for loop竞争设置为false
bool selected = false;
Next, create a while loop around the switch, set to loop while the bool "selected" is false 接下来,在开关周围创建一个while循环,设置为在“ selected”布尔值为false时循环
while(!selected){
switch(type)
Then change the bool to true after a successful user selection 成功选择用户后,将布尔值更改为true
case 'p':
case 'P':
selected = true;
Try this code, its very simple: 试试下面的代码,它非常简单:
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!");
}
}
}
All I added is while loop with Boolean "true", a continue and break statement. 我只添加了while循环和布尔“ true”(一个continue和break语句)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.