所以,我对java很新,我想在命令行中尝试调试,即在有一些错误的代码上使用jdb。我正在修改这段代码:
import java.util.Scanner;
import java.io.File;
class LetterHome{
static final int MAX_CODE = 5;
public static void main(String[] args) throws Exception{
Scanner in = new Scanner(new File(args[0]));
String phrase;
int sentenceCode, modifierCode;
System.out.println("Dear Mom and Dad:\n");
while( in.hasNext() ){
sentenceCode = in.nextInt();
modifierCode = in.nextInt();
if( (sentenceCode < 1) || (sentenceCode >= MAX_CODE) ) {
System.out.println(sentenceCode + " is not a valid sentence code");
continue;
}
if( sentenceCode == 1 ){
if( modifierCode == 1 ){
phrase = "great";
}else if( modifierCode == 2 ){
phrase = "ok";
}else{
phrase = "ERROR";
}
System.out.println("My classes are going " + phrase + ".");
}else if( sentenceCode == 2 ){
phrase = weatherModifier(modifierCode);
System.out.println("The weather here has been " + phrase + ".");
}else if( sentenceCode == 3 ){
if( modifierCode == 1 ){
phrase = "after the quarter ends";
}else if( modifierCode == 2 ){
phrase = "in a few weeks";
}else if( modifierCode == 3 ){
phrase = "next weekend";
}else{
phrase = "ERROR";
}
System.out.println("I plan to come home for a visit " + phrase + ".");
}else if( sentenceCode == 4 ){
System.out.println("Do you think you could send me $" + modifierCode + "?");
System.out.println("I have to buy another book for one of my classes.");
}else if( sentenceCode == 5 ){
if( modifierCode == 1 ){
phrase = "cookies";
}else if( modifierCode == 2 ){
phrase = "stuff";
}else; if( modifierCode == 3 ){
phrase = "money";
}else{
phrase = "ERROR";
}
System.out.println("Thanks for the " + phrase + " you sent.");
}
}
}
static String weatherModifier(int m) {
String word=null;
if(m == 1)
word = "great";
if(m == 2)
word = "foggy";
if(m == 3)
word = "hot";
if(m == 4)
word = "cold";
if(m == 5)
word = "variable";
if( m<1 && m>5)
word = "ERROR";
return word;
}
}
我已经找到了&#34 ;;&#34;那是不合适的。我知道有些东西与最终分配给术语的值有关(我认为),因为当我编译程序时,我得到了这个输出:
Dear Mom and Dad:
//
// 5 is not a valid sentence code
// My classes are going great.
// The weather here has been foggy.
// I plan to come home for a visit in a few weeks.
而不是上课和#34;伟大&#34;,我得到&#34;模糊&#34;,我在运行jdb时注意到了。我用这个数据文件运行代码:
5 2
1 1
2 1
3 2
代码本身会打印一个模板,其中包含数据文件中列出的可能选项,您可能已经注意到了这些选项。以下是完整的可能性列表:
// 1. My classes are going _____.
// 1. great
// 2. ok
// 2. The weather here has been _____.
// 1. great
// 2. foggy
// 3. hot
// 4. cold
// 5. variable
// 3. I plan to come home for a visit _____.
// 1. after the quarter ends
// 2. in a few weeks
// 3. next weekend
// 4. Do you think you could send me $_____?
// I have to buy another book for one of my classes.
// 5. Thanks for the _____ you sent.
// 1. cookies
// 2. stuff
// 3. money
但是,我不确定代码中的逻辑是什么。有小费吗?如果我需要澄清,请告诉我。感谢。
答案 0 :(得分:0)
您在;
功能的if (m==2);
末尾添加了额外的weatherModifier
。删除它!
答案 1 :(得分:0)
我想出了问题,实际上:
它在&#34; if((sentenceCode&lt; 1)||(sentenceCode&gt; = MAX_CODE))&#34;
我不得不改变&#34;&gt; =&#34;到&#34;&gt;&#34;