[英]Doing a beginner's course on Java, having problems with do-while and if-else loops, as well as string validation
我正在上一门关于 Java 的初学者课程,目前没有人可以寻求帮助。
该程序很简单,您输入您的详细信息,该程序将为您提供汽车保险的报价。
这是我写的
System.out.print("Please enter the age of the driver: ");
age = Genio.getInteger();
if ( age >18 || age < 70)
{
switch (age)
{
case 17: case 18: case 19:
addition = 460.00;
quote = premium + addition;
return quote;
case 20: case 21:
addition = 280.00;
quote = premium + addition;
return quote;
case 22: case 23: case 24: case 25:
addition = 180.00;
quote = premium + addition;
return quote;
case 26: case 27: case 28:
addition = 140.00;
quote = premium + addition;
return quote;
case 60: case 61: case 62: case 63: case 64: case 65:
addition = 85.00;
quote = premium + addition;
return quote;
case 66: case 67: case 68: case 69:
addition = 150.00;
quote = premium + addition;
return quote;
case 70: case 71: case 72: case 73: case 74: case 75: case 76: case 77: case 78: case 79:
addition = 300.00;
quote = premium + addition;
return quote;
case 80: case 81: case 82: case 83: case 84: case 85:
addition = 800.00;
quote = premium + addition;
return quote;
default:
quote = 400.00;
return quote;
}
} else {
System.out.print("Your are too young to apply for a premium. Sorry");
}
return age;
我无法想象为什么这行不通。 当我输入一个无效的年龄(例如 12 岁)时,程序会返回 400 英镑的默认报价。 当我删除默认情况时,它返回的报价为 12 英镑。 所以它似乎完全忽略了 if else 循环。
(我知道 switch-case 是一种效率低下的方法,但这是任务的一部分。)
例如。 我输入 MR 作为标题。
程序打印“无效响应。使用‘MR’、‘MRS’或‘MS’请输入您的标题”
我再次输入MR。 程序进入下一阶段。
我创建了一个方法来验证我在这里写的标题。
public String validateTitle(String title)
{
System.out.print("Please enter your title ");
title = Genio.getString();
title = title.toUpperCase();
do
{
if(title.equals("MR") && (title.equals("MRS") && (title.equals("MS"))))
{
return title;
}
}while((title.equals("MR") && (title.equals("MRS")) && (title.equals("MS"))));
do
{
System.out.print("Invalid Response. Use 'MR', 'MRS' or 'MS' ");
System.out.print("Please enter your title ");
title = Genio.getString();
if((!title.equals("MR") && (!title.equals("MRS")) && (!title.equals("MS"))))
{
System.out.println("Invalid Response. Use 'MR', 'MRS' or 'MS' ");
}
if(title.equals("MR") && (title.equals("MRS") && (title.equals("MS"))))
{
break;
}
}while((!title.equals("MR") && (!title.equals("MRS")) && (!title.equals("MS"))));
return title;
}
任何人都可以发现问题是什么?
这些方法
public String validateForename(String forename)
{
System.out.print("Please enter your forename ");
forename = Genio.getString();
forename = forename.toUpperCase();
if(forename != null && !forename.isEmpty())
System.out.println("Invalid Response.");
return forename;
}
public String validateSurname(String surname)
{
System.out.print("Please enter your surname ");
surname = Genio.getString();
surname = surname.toUpperCase();
if(surname != null && !surname.isEmpty())
System.out.println("Invalid Response.");
return surname;
}
我采用的一段代码是if(xx != null && !xx.isEmpty()) 。 我以为我需要删除“!”,但这样做会阻止程序编译。 有任何想法吗?
我很抱歉问这个问题,但我的导师今天无法解释我哪里出错了。
if ( age >18 || age < 70)
错了,因为12
小于70
。
你需要说和( &&
):
if ( age >18 && age < 70)
但正好是 18 或 70 也是有效的,所以:
if (age >= 18 && age <= 70)
同样,你有&&
和||
使困惑。
if(title.equals("MR") && (title.equals("MRS") && (title.equals("MS"))))
读作:如果title 等于mr 且title 等于mrs 且title 等于ms。
title
不能是所有这些。
if(surname != null && !surname.isEmpty())
System.out.println("Invalid Response.");
您的意思是说如果 surname 为 null 或 surname 为空:
if (surname == null || surname.isEmpty())
System.out.println("Invalid Response.");
您应该在if loop
添加&&
运算符。
if ( age >18 && age < 70)
希望这会有所帮助!
至于问题2:
你得到 && 和 || 使困惑。 这里:
if(title.equals("MR") && (title.equals("MRS") && (title.equals("MS"))))
标题永远不能同时是“MR”、“MRS”和“MS”,您要使用 || 代替 &&。
编辑:
你的#2 的整个代码有点臃肿。 这是我的方法:
public String validateTitle()
{
System.out.print("Please enter your title ");
String title = "";
do
{
title = Genio.getString().toUpperCase();
if (!title.matches("(MR|MRS|MS"))
System.out.print("Invalid Response. Use 'MR', 'MRS' or 'MS' ");
} while (!title.matches("(MR|MRS|MS"));
return title;
}
你能解释一下,为什么你的函数得到一个字符串作为参数,它会立即覆盖吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.