필자는 personalize라는 필기 방법이 있습니다. 이것이 지금까지 해왔 던 것입니다.방법을 이해하려고 시도합니다.
// personalize takes a String and validates it
// if it is a valid plate, it changes the plateNumber to
// the new plateNumber and calculates the cost of the plate.
// the method returns true if the new plateNumber is valid and the plate was changed,
// and false if the new plateNumber was invalid and no changes were made.
// A personalized plate may be 3 up to 7 chars and 1 space or dash
// Use letters, numbers, dashes, and spaces ONLY
// A personalized plate costs $10 extra plus $5 per letter (not including dashes or spaces)
public boolean personalize(String vanity)
{
boolean valid = true;
vanity = "";
for(int i = 0; i < vanity.length(); i++)
{
if(vanity.length() < 7 && vanity.length() > 3)
{
valid = true;
}
if(Character.isLowerCase(vanity.charAt(i)) || vanity.length() > 7 || vanity.length() < 3 ||
vanity.charAt(i) == '!' || vanity.charAt(i) == '.' || vanity.charAt(i) == '$' ||
vanity.charAt(i) == '#' || vanity.charAt(i) == '*' || vanity.charAt(i) == '_' ||
vanity.charAt(i) == '@' || vanity.charAt(i) == '^' || vanity.charAt(i) == '&')
{
valid = false;
}
}
if(valid = true)
{
plateCost += 25;
}
return valid;
}
이 방법에서 내가 가진 모든 것이 완전히 정확하지는 않지만 나는 그것에 대해 매우 혼란스러워합니다. 헬퍼 메소드 작성에 대해 생각하고 있었지만 비용 (newCost) 또는 새 플레이트 번호 (personalizedPlate)에 대한 확신이 없습니다. 아니면 둘 다 써야합니까? 나는 단순히 내 일에 대한 해답을 찾는 것이 아니다. 나는 정말로 내가해야 할 일을 더 잘 이해하고 내가 왜 그런 식으로해야 하는지를 이해하기 위해 문제를 통해 나를 도울 사람을 찾고 있습니다.
죄송합니다. StackOverflow는 이러한 종류의 질문을 의미하지 않습니다. 그것은 "문제를 통해 나를 도와주세요"와 같은 일반적인 것들이 아닌 특정한 질문을하기위한 것입니다. 이를 위해서는 토론 그룹 (우리는 아니지만)이나 교사가 필요하다고 생각합니다. – ajb
이 코드는 올바르지 않습니다. 평등에 대한 테스트 대신 할당을 수행 할 때'if (valid = true) '입니다. –
'valid = false'로 시작해야합니다. 'valid = false' 인 경우 루프와 내부 루프를 길이 체크 아웃해야합니다. – Ambrish