已给出此代码,并被要求解决此代码。需要帮助来理解如何解决此问题。
class Main {
public static void main(String[] args) {
// Should print 7
System.out.println(stringLength("request")); // this line cannot be changed
// Should print "request must not be empty"
System.out.println(stringLength("")); // this line cannot be changed
// Should print "request is required"
System.out.println(stringLength(null)); // this line cannot be changed
}
public static Integer stringLength(String request) // this line cannot be changed
{}
答案 0 :(得分:0)
评论说您“应该”进行打印,但是没有任何内容表明您在打印后不能再打印其他任何东西。什至没有强迫进行打印,否则将是必须。
说实话,阅读诸如此类的有关作业的问题会让我不高兴。
对我来说,整个过程是无用的,丑陋的,并且没有教给将来的现实世界软件设计和编码任何有价值的东西。它所教的一件事是:给定差的规格,做不好的工作来取悦客户。 嗯...这可能是一个很好的学习课程?
由于您没有提供所尝试内容的任何代码,也没有提供任何有关您所理解内容的暗示,因此我可以自由地提出某种解决方案,以执行所需的打印。
因此,这里有3种实现。我试图粗略地做到这一点。也许其他人会发现其他奇怪的方法来做到这一点。
import java.util.HashMap;
import java.util.Map;
public class Main {
private static final String REQUEST_MUST_NOT_BE_EMPTY = "request must not be empty";
private static final String REQUEST_IS_REQUIRED = "request is required";
private static final String REQUEST = "request";
private static final String EMPTY_STRING = "";
private static final Map<String, Object> requestMessageMap = new HashMap<String, Object>();
static {
// requestMessageMap.put(REQUEST, Integer.valueOf(7));
// requestMessageMap.put(REQUEST, Integer.valueOf(REQUEST.length()));
requestMessageMap.put(EMPTY_STRING, REQUEST_MUST_NOT_BE_EMPTY);
requestMessageMap.put(null, REQUEST_IS_REQUIRED);
}
public static void main(String[] args) {
// Should print 7
System.out.println(stringLength("request")); // this line cannot be changed
// Should print "request must not be empty"
System.out.println(stringLength("")); // this line cannot be changed
// Should print "request is required"
System.out.println(stringLength(null)); // this line cannot be changed
}
public static Integer stringLength(String request) // this line cannot be changed
{
return sillyMethod4(request);
}
private static Integer sillyMethod1(String request) {
Integer returnValue = -1;
if (request == null) {
// do exactly what specification required
// (completly pointeless)
System.err.println(REQUEST_IS_REQUIRED);
} else if (request.equals(EMPTY_STRING)) {
// do exactly what specification required
// (completly pointeless)
System.err.println(REQUEST_MUST_NOT_BE_EMPTY);
} else if (request.equals(REQUEST)) {
// do exactly what specification required
// (completly pointeless)
returnValue = 7;
} else {
// my best guess about what we should really do
returnValue = request.length();
}
return returnValue;
}
private static Integer lessSillyMethod2(String request) {
Integer returnValue = -1;
if (request == null) {
// do exactly what specification required
// (completly pointeless)
System.err.println(REQUEST_IS_REQUIRED);
} else if (request.equals(EMPTY_STRING)) {
// do exactly what specification required
// (completly pointeless)
System.err.println(REQUEST_MUST_NOT_BE_EMPTY);
} else {
// my best guess about what we should really do
returnValue = request.length();
}
return returnValue;
}
private static Integer sillyMethod3(String request) {
Integer returnValue = -1;
if (request == null) {
// do exactly what specification required
// (completly pointeless)
System.err.println("request is required: ");
} else {
switch (request) {
case EMPTY_STRING:
// do exactly what specification required
// (completly pointeless)
System.err.println("request must not be empty: ");
break;
case REQUEST:
// do exactly what specification required
// (completly pointeless)
returnValue = 7; //
break;
default:
// my best guess about what we should really do
returnValue = request.length();
break;
}
}
return returnValue;
}
private static Integer sillyMethod4(String request) {
Integer returnValue = -1;
if (requestMessageMap.containsKey(request)) {
System.err.println(requestMessageMap.get(request));
} else {
returnValue = request.length();
}
return returnValue;
}
}
输出:
7
request must not be empty
request is required
-1
-1
答案 1 :(得分:-3)
stringLengt()方法在将字符串“ request”传递给它时应返回“ 7”,在传递空字符串时应返回不同的值,而在传递null时应返回另一个不同的值。也许您应该看看控制结构,尤其是选择项。另外,您的stringLength方法还需要返回String类型的值。