public static void main(String[] args) {
String Day; // The user’s name.
TextIO.put("This program is to tell you what to eat for today.");
TextIO.put("\nBefore it starts, can you tell me what's the day today?");
int count;
int CCount;
String Answer;
CCount = 0;
count = 0;
do {
Day = TextIO.getln();
if (Day.equals("Monday")) {
System.out.print("Eat Chocolates");
break;
} else if (Day.equals("Mon")) {
System.out.print("Eat Chocolates");
break;
} else if (Day.equals("MON")) {
System.out.print("Eat Chocolates");
break;
} else if (Day.equals("mon")) {
System.out.print("Eat Chocolates");
break;
} else if (Day.equals("monday")) {
System.out.print("Eat Chocolates");
break;
} else if (Day.equals("MONDAY")) {
System.out.print("Eat Chocolates");
break;
} else if (Day.equals(false)); // No necessary , can be delete
count = count + 1;
System.out.print("Fail to proceed #" + count);
if (count == 3) {
System.out.print("\nProgram Terminate");
System.exit(0);
} else {
System.out.print("\nPlease re-eneter the answer : ");
}
} while (true);
System.out.print("\nNow's can you tell me what's your horoscope?");
String Horoscope;
String Name;
String CrazyThing;
double Salary;
double Annually;
do {
Horoscope = TextIO.getln();
if (Horoscope.equals("Libra")) {
System.out.print("Seriously?");
System.out.print("\nYou are?");
Name = TextIO.getln();
System.out.print("Hi! " + Name + " How are you~");
break;
} else if (Horoscope.equals("Gemini")) {
System.out.print("Oh,okay~ Nice to meet you.");
break;
} else if (Horoscope.equals("Aries")) {
System.out.print("Wow, you're rich~");
System.out.print("\nNow, tell me how much you've earned per month?");
Salary = TextIO.getlnDouble();
System.out.print("Hmmmmm $ " + Salary + " , right?");
Annually = Salary * 12;
System.out.print("\nYour annually salary are ");
System.out.printf("$ " + "%.2f", Annually);
break;
} else if (Horoscope.equals("Leo")) {
System.out.print("You're full of courages~");
System.out.print("Now, tell me ~ What's the most crazy thing you ever do before?");
CrazyThing = TextIO.getln();
System.out.print(CrazyThing + "?");
break;
} else if (Horoscope.equals(true)); //Not necessary, can be delete
CCount = CCount + 1;
System.out.print("Fail to proceed #" + CCount);
if (CCount == 3) {
System.out.print("\nProgram Terminate");
System.exit(0);
} else {
System.out.print("\nPlease re-eneter your horoscope again : ");
}
} while (true);
System.out.println("Do you wish to restart the program again?");
Answer = TextIO.getln();
if (Answer.equals("Yes"));
}
我想在这篇文章中提出两个问题(对不起,如果我的问题可能看起来很愚蠢,但我真的需要帮助)。
问题1:如何重新启动程序?在星座运势结束时,我想询问用户是否想重新开始重新开始。
问题2:是否有任何方法可以通过字母表简化代码? 例如在我的代码中,我设置了MON和mon和Mon,是否有任何方法可以将这三者合并为一个?
答案 0 :(得分:0)
我目前没有时间回答问题一,但问题二是
if(day.toLowerCase().equals("mon")){ /*this makes the user input all lowercase,
so you dont have to detect case anomalies*/
}
根据UnknownOctopus的建议,你也可以使用
if(day.equalsIgnoreCase("mon"))
你会得到同样的效果。
修改强>
对于问题1,您需要创建一个名为wantsToPlay
的布尔值或类似的布尔值,并将其设置为true(boolean wantsToPlate = true;
。
然后你把你的程序的其余部分放到一个说
while(wantsToPlay){
//program
//ask user if they want to play again
//if input is yes or yay or whatever then{
//wantsToPlay = true;
//}else{
//wantsToPlay = false;
}