[英]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.