我有两个循环似乎无法正常工作。基本上,如果我写错了东西,它会把我发送到我必须正确写它的地方。当我到达那一点时,无论我输入什么,我都不能留下输入。任何说“无效”的行都没有给我我需要的结果。请帮帮我!这是代码。
import java.util.Arrays;
import java.util.Scanner;
public class EZRealEstateSimulator {
public static void main(String[] args) {
/* Declarations */
String buy = "Buy";
String rent = "Rent";
String sell = "Sell";
String quit = "Quit";
String [] availableHomes;
availableHomes = new String [6];
availableHomes [0] = "516 Dunkin St, Ontario, CA";
availableHomes [1] = "224 Mooclid Ave, Ontario, CA";
availableHomes [2] = "342 Pine Ave, Chino Hills, CA";
availableHomes [3] = "1488 Overprice Ave, Chino Hills, CA";
availableHomes [4] = "632 Clayton Drive, Chino Hills, CA";
availableHomes [5] = "778 Lakeside Ct, Rancho Cucamonga, CA";
String [] availableApartments;
availableApartments = new String [6];
availableApartments [0] = "1248 Helm St, APT: 678, Rancho Cucamonga,CA";
availableApartments [1] = "892 Low Park Ave, APT:32, Rancho Cucamonga,CA";
availableApartments [2] = "120 Maven Ct, APT: 55, Ontario,CA";
availableApartments [3] = "423 Scimitar St, APT: 12, Ontario,CA";
availableApartments [4] = "918 Mystic Parkway, APT:3, Rancho Cucamonga,CA";
availableApartments [5] = "1316 Cobalt Ct, APT: 27, Chino Hills,CA";
String [] activeCities;
activeCities = new String [3];
activeCities [0] = "Ontario";
activeCities [1] = "Rancho Cucamonga";
activeCities [2] = "Chino Hills";
String house = "House";
String apartment = "Apartment";
int [] paymentPlan;
paymentPlan = new int [4];
paymentPlan [0] = 250;
paymentPlan [1] = 500;
paymentPlan [2] = 750;
paymentPlan [3] = 1000;
int downPayment = 200;
String completeReport;
int changeLoop;
int changeAltLoop;
String customerAddress = " ";
String customerCellNumber = " ";
String customerHomeNumber = " ";
String customerName = " ";
String housingTenure;
String livingArea;
String cities;
String purchasedHome = " ";
String purchasedApartment = " ";
int errorLoop;
int gameLoop;
Scanner input = new Scanner (System.in);
/* Personal Info Prompts */
System.out.println("Would you like to buy, or quit program? Type Buy or Quit ");
housingTenure = input.nextLine ();
System.out.println("Would this be for an apartment or home? Type House or Apartment");
livingArea = input.nextLine ();
System.out.println("Please enter your full name: ");
String name = input.nextLine ();
customerName = name;
System.out.println("Please enter your full address: ");
String address = input.nextLine ();
customerAddress = address;
System.out.println("Please enter your home number: ");
String hNumber = input.nextLine ();
customerHomeNumber = hNumber;
System.out.println("Please enter your cell number: ");
String cNumber = input.nextLine ();
customerCellNumber = cNumber;
gameLoop = 0;
while (gameLoop == 0) {
/* Buy */
changeLoop = 0;
while (changeLoop == 0 && livingArea.equals(house)) {
System.out.println("Which city would you like to buy property in? Type Ontario, Chino Hills, or Rancho Cucamonga ");
cities = input.nextLine ();
if (!cities.equals(activeCities [0]) || !cities.equals(activeCities [1]) || !cities.equals(activeCities [2])) {
System.out.println("Invalid. Please enter either Ontario, Chino Hills, or Rancho Cucamonga");
cities = input.nextLine ();
} else {
continue;
}
if (cities.equals(activeCities [0])) {
System.out.println("The available homes are: " + availableHomes [0] + " and " + availableHomes [1] + " .Please select one by typing availableHomes [#] ");
purchasedHome = input.nextLine ();
if (!purchasedHome.equals(availableHomes[0]) || !purchasedHome.equals(availableHomes [1])) {
System.out.println("Invalid");
purchasedHome = input.nextLine ();
} else {
changeLoop += 1;
}
} else if (cities.equals(activeCities [1])) {
System.out.println("The available homes are: " + availableHomes [5] + " .Please select one by typing availableHomes [#] ");
purchasedHome = input.nextLine ();
if (!purchasedHome.equals(availableHomes[5])) {
System.out.println("Invalid");
purchasedHome = input.nextLine ();
} else {
changeLoop += 1;
}
} else {
System.out.println("The available homes are: " + availableHomes [2] + " , " + availableHomes [3] + " and " + availableHomes [4] + " .Please select one by typing availableHomes [#] ");
purchasedHome = input.nextLine ();
if (!purchasedHome.equals(availableHomes[2]) || !purchasedHome.equals(availableHomes [3]) || !purchasedHome.equals(availableHomes [4])) {
System.out.println("Invalid");
purchasedHome = input.nextLine ();
} else {
changeLoop += 1;
}
}
}
changeAltLoop = 0;
while (changeAltLoop == 0 && livingArea.equals(apartment)) {
System.out.println("Which city would you like to buy property in? Type Ontario, Chino Hills, or Rancho Cucamonga ");
cities = input.nextLine ();
if (!cities.equals(activeCities [0]) || !cities.equals(activeCities [1]) || !cities.equals(activeCities [2])) {
System.out.println("Invalid. Please enter either Ontario, Chino Hills, or Rancho Cucamonga");
cities = input.nextLine ();
}
if (cities.equals(activeCities [0])) {
System.out.println("The available apartments are: " + availableApartments [2] + " and " + availableApartments [3]);
purchasedApartment = input.nextLine ();
if (!purchasedApartment.equals(availableApartments[2]) || !purchasedApartment.equals(availableApartments [3])) {
System.out.println("Invalid");
purchasedApartment = input.nextLine ();
} else if (cities.equals(activeCities [1])) {
System.out.println("The available apartments are: " + availableApartments [0] + " , " + availableApartments [1] + " and " + availableApartments [4]);
purchasedApartment = input.nextLine ();
if (!purchasedApartment.equals(availableApartments [0]) || !purchasedApartment.equals(availableApartments [1])) {
System.out.println("Invalid");
purchasedApartment = input.nextLine ();
}
} else {
System.out.println("The available apartments are: " + availableApartments [5]);
purchasedApartment = input.nextLine ();
if (!purchasedApartment.equals(availableApartments [5])) {
System.out.println("Invalid");
purchasedApartment = input.nextLine ();
} else {
changeAltLoop += 1;
}
}
}
}
/* eof */
System.out.println("Please select a monthly payment plan price: " + paymentPlan [0] + " , " + paymentPlan [1] + " , " + paymentPlan [2] + " and " + paymentPlan [3]);
System.out.println("REMAINDER! Each payment is due on the 5th of each month and a $200 down payment is required.");
int payment = input.nextInt ();
System.out.println("Processing data into detailed report...");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("---Complete Report---");
System.out.println("Customer Name: " + name);
System.out.println("Customer Address: " + address);
System.out.println("Customer Home Number: " + hNumber);
System.out.println("Customer Cell Number: " + cNumber);
System.out.println("Customer Payment Plan: " + payment);
System.out.println("Customer Purchased House: " + purchasedHome);
System.out.println("Customer Purchased Apartment: " + purchasedApartment);
}
}
}
答案 0 :(得分:1)
这是很多代码:) 那么你说的问题是由于你的if条件
if (!cities.equals(activeCities[0]) || !cities.equals(activeCities[1]) || !cities.equals(activeCities[2])) {
System.out.println("Invalid. Please enter either Ontario, Chino Hills, or Rancho Cucamonga");
cities = input.nextLine();
} else {
continue;
}
由于您正在使用OR(||)操作,因此它会经历所有条件,直到找到true。因此,为了跳过此循环,所有表达式都应返回false,这意味着输入的城市应该等于所有不可能的activeCities。所以它一直都会显示无效。所以首先将逻辑改为
if (!cities.equals(activeCities[0]) && !cities.equals(activeCities[1]) && !cities.equals(activeCities[2])) {
System.out.println("Invalid. Please enter either Ontario, Chino Hills, or Rancho Cucamonga");
cities = input.nextLine();
}
另请注意其他情况。你应该删除整个其他因为这里的continue语句导致重新迭代while循环,只要你输入有效的城市,它就会一遍又一遍地问同一个问题。
快速提示
equalsIgnoreCase
比较会更好,因为用户可能会
键入“house”而不是“House”if(!activeCities.contains(cities))
。代码将简洁明了
清洁剂。完整示例
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class EZRealEstateSimulator {
private static final Set<String> activeCities, propertyOptions, commands;
private static final Map<String, Set<String>> availableHomes, availableApartments;
private static final String[] prompts = new String[9];
private static final int[] paymentPlan = { 250, 500, 750, 1000 };
private static final int downPayment = 200;
private static String userOption, customerAddress, customerCellNumber, customerHomeNumber, customerName, propertyType, city, purchasedHome, purchasedApartment, customerPaymentPlan;
private static int i = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
do {
System.out.println(getPrompt(i));
String value = input.nextLine();
if(!isValidOption(value, i)){
System.out.println("Invalid Option!!");
continue;
}
i++;
} while(i < 9);
input.close();
System.out.println("Processing data into detailed report...");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("---Complete Report---");
System.out.println("Customer Name: " + customerName);
System.out.println("Customer Address: " + customerAddress);
System.out.println("Customer Home Number: " + customerHomeNumber);
System.out.println("Customer Cell Number: " + customerCellNumber);
System.out.println("Customer Payment Plan: " + customerPaymentPlan);
System.out.println("Customer Purchased House: " + purchasedHome);
System.out.println("Customer Purchased Apartment: " + purchasedApartment);
}
private static String getPrompt(int index) {
String message = prompts[index];
switch (index) {
case 6: //City Question
message = String.format(message, activeCities.toString());
break;
case 7: //Available house/apartment question
String opts = ("House".equalsIgnoreCase(propertyType) ? join(availableHomes.get(city)) : join(availableApartments.get(city)));
message = String.format(message, propertyType, opts);
break;
case 8: //Payment Question
message = String.format(message, Arrays.toString(paymentPlan), downPayment);
break;
default:
break;
}
return message;
}
private static boolean isValidOption(String value, int index) {
boolean isValid = true;
switch (index) {
case 0:
userOption = value;
isValid = commands.contains(value.toUpperCase());
if(isValid && "Quit".equalsIgnoreCase(userOption)){
i = 10; // Breaks out of main while loop.
}
break;
case 1:
propertyType = value;
isValid = propertyOptions.contains(value.toUpperCase());
break;
case 2:
customerName = value;
break;
case 3:
customerAddress = value;
break;
case 4:
customerHomeNumber = value;
break;
case 5:
customerCellNumber = value;
break;
case 6:
city = value;
isValid = activeCities.contains(value);
break;
case 7:
if ("House".equalsIgnoreCase(propertyType)) {
purchasedHome = value;
isValid = availableHomes.get(city).contains(value);
} else {
purchasedApartment = value;
isValid = availableApartments.get(city).contains(value);
}
break;
case 8:
customerPaymentPlan = value;
try {
if (Arrays.binarySearch(paymentPlan, Integer.parseInt(value)) == -1) {
isValid = false;
}
} catch (NumberFormatException e) {
isValid = false;
}
break;
default:
break;
}
return isValid;
}
public static String join(Set<String> array) {
final int noOfItems = array.size();
final StringBuilder buf = new StringBuilder(noOfItems * 16);
int idx = 0;
for (String value : array) {
if (idx > 0) {
buf.append("\n");
}
idx++;
buf.append(idx + ". " + value);
}
return buf.toString();
}
// Initialize constants
static {
availableHomes = new TreeMap<String, Set<String>>(String.CASE_INSENSITIVE_ORDER);
availableHomes.put("Ontario", new TreeSet<String>(String.CASE_INSENSITIVE_ORDER) {{ add("516 Dunkin St, Ontario, CA"); add("224 Mooclid Ave, Ontario, CA"); }});
availableHomes.put("Chino Hills", new TreeSet<String>(String.CASE_INSENSITIVE_ORDER) {{ add("342 Pine Ave, Chino Hills, CA"); add("1488 Overprice Ave, Chino Hills, CA"); add("632 Clayton Drive, Chino Hills, CA"); }});
availableHomes.put("Rancho Cucamonga", new TreeSet<String>(String.CASE_INSENSITIVE_ORDER) {{ add("778 Lakeside Ct, Rancho Cucamonga, CA"); }});
availableApartments = new TreeMap<String, Set<String>>(String.CASE_INSENSITIVE_ORDER);
availableApartments.put("Rancho Cucamonga", new TreeSet<String>(String.CASE_INSENSITIVE_ORDER) {{ add("1248 Helm St, APT: 678, Rancho Cucamonga,CA"); add("892 Low Park Ave, APT:32, Rancho Cucamonga,CA"); add("918 Mystic Parkway, APT:3, Rancho Cucamonga,CA"); }});
availableApartments.put("Ontario", new TreeSet<String>(String.CASE_INSENSITIVE_ORDER) {{ add("120 Maven Ct, APT: 55, Ontario,CA"); add("423 Scimitar St, APT: 12, Ontario,CA"); }});
availableApartments.put("Chino Hills", new TreeSet<String>(String.CASE_INSENSITIVE_ORDER) {{ add("1316 Cobalt Ct, APT: 27, Chino Hills,CA"); }});
activeCities = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
activeCities.addAll(Arrays.asList(new String[] {"Ontario", "Rancho Cucamonga", "Chino Hills"}));
propertyOptions = new HashSet<String>(Arrays.asList(new String[] { "HOUSE", "APARTMENT" }));
commands = new HashSet<String>(Arrays.asList(new String[] { "BUY", "QUIT" }));
prompts[0] = "Would you like to buy, or quit program? Type Buy or Quit";
prompts[1] = "Would this be for an apartment or home? Type House or Apartment";
prompts[2] = "Please enter your full name: ";
prompts[3] = "Please enter your full address: ";
prompts[4] = "Please enter your home number: ";
prompts[5] = "Please enter your cell number: ";
prompts[6] = "Which city would you like to buy property in? Options %s :";
prompts[7] = "The available %s are:\n%s\nPlease select one by typing availableHomes [#] ";
prompts[8] = "Please select a monthly payment plan price: %s \n REMAINDER! Each payment is due on the 5th of each month and a $%s down payment is required.";
}
}