d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I'm Stumped... What's Wrong With My Code
Add Reply New Topic New Poll
Member
Posts: 31,292
Joined: Mar 25 2009
Gold: 0.00
Feb 27 2017 03:07am
EDIT: Nvm fixed code :)

Code

class Help {
void helpOn(int what) {
switch(what) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" case constant:");
System.out.println(" statement sequence");
System.out.println(" break;");
System.out.println(" //...");
System.out.println("}");
break;
case '3':
System.out.println("The for:\n");
System.out.println("for(init; condition; iteration)");
System.out.println(" statement;");
break;
case '4':
System.out.println("The while:\n");
System.out.println("while(condition) statement;");
break;
case '5':
System.out.println("The do-while:\n");
System.out.println("do {");
System.out.println(" statement;");
System.out.println("} while(condition);");
break;
case '6':
System.out.println("The break:\n");
System.out.println("break; or break label;");
break;
case '7':
System.out.println("The continue:\n");
System.out.println("continue; or continue label;");
break;
}
System.out.println();
}

void showMenu() {
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. for");
System.out.println(" 4. while");
System.out.println(" 5. do-while");
System.out.println(" 6. break");
System.out.println(" 7. continue\n");
System.out.println("Choose one (q to quit): ");
}

boolean isValid(int ch) {
if(ch < '1' | ch > '7' & ch != 'q') return false;
else return true;
}
}

class HelpClassDemo {
public static void main(String args[])
throws java.io.IOException {
char choice, ignore;
Help hlpobj = new Help();

for(;;) {
do {
hlpobj.showMenu();
choice = (char) System.in.read();
do {
ignore = (char) System.in.read();
} while(ignore != '\n');
} while ( !hlpobj.isValid(choice) );

if(choice == 'q') break;
System.out.println("\n");
hlpobj.helpOn(choice);

}


}
}


This post was edited by ferf on Feb 27 2017 03:34am
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Feb 27 2017 11:25am
quick glance says that your case statement is fucked
'1' = 48
the character '1' and the integer 1 are different

you should get in the habit of putting defaults in all your switches to print out an error if something weird goes wrong

This post was edited by Ideophobe on Feb 27 2017 11:30am
Member
Posts: 145
Joined: Aug 6 2016
Gold: 0.45
Mar 4 2017 09:35pm
make a variable in switch that takes the variable what into a char, then fix the cases to be that variable = w/e.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll