java - Converting Program With Only A Constructor To One With A Main Method Instead -
my instructor having trouble grading assignment. created following program in bluej, , uses eclipse. problem couldn't bluej fire piece of code main method, opted use constructor instead. copy/paste of bluej code eclipse shows problematic way is:
error: not find or load main class misspelled
how convert constructor-only program below program working main method? don't need constructor more.
import java.util.scanner; public class randomgame { int usersscore = 0; public randomgame() { scanner userinput = new scanner(system.in); string[] wrongwords = {"mispelled","kobra", "wishfull", "adress", "changable","independant", "emberrass", "cieling", "humerous", "wierd"} ; string[] rightwords = {"arctic", "miscellaneous", "piece", "prejudice", "grateful","ecstasy", "fascinate", "definite", "changeable", "conscious"}; double randomnumber; int randomnumberint; string wordtocheck; //display rules system.out.printf("enter either 'y' or 'n'"); //keep looping game while(usersscore < 5) { //generate random number randomnumber = generaterandomnumber(); //math.round(10 * math.random()); randomnumberint = ((int)(randomnumber)); //display user's score system.out.printf("\n\ncurrent score: %d\n", usersscore); //check place value of random number in array if(randomnumber %2 == 0) { wordtocheck = rightwords[randomnumberint]; system.out.printf("correct?: %s\n", wordtocheck); rightcheck(userinput.next()); } else { wordtocheck = wrongwords[randomnumberint]; system.out.printf("correct?: %s\n", wordtocheck); wrongcheck(userinput.next()); } } //system.out.printf(wordtocheck); } public double generaterandomnumber() { double randnum; randnum = math.round(10 * math.random()); return(randnum); } public boolean rightcheck(string usersanswer) { if(usersanswer.equals("y")) { system.out.printf("correct! rightcheck"); usersscore++; return(true); } else { system.out.printf("incorrect rightcheck"); usersscore--; return(false); } } public boolean wrongcheck(string usersanswer) { if(usersanswer.equals("n")) { system.out.printf("correct! wrongcheck"); usersscore++; return(true); } else { system.out.printf("incorrect wrongcheck"); usersscore--; return(false); } } }
one way create class hold main method
public class main { public static void main (string args[]) { randomgame rgame = new randomgame(); } }
(note: name of .java file must main.java in above case)
otherwise can add main() randomgame class in following way
public static void main (string args[]) { randomgame rgame = new randomgame(); }
Comments
Post a Comment