I have this wierd kink in this program, it keeps telling me that there is a "missing return statement for public static question. i'm kind of new to JAVA and i've tried everything! please can someone help!

import javax.swing.JOptionPane;
public class arithmetic {
    public static void main(String[] args) {
   JOptionPane.showMessageDialog(null, "Welcome student!");
   int correct=0;
   int wrong=0;
   char yon;
   boolean answer;

   do{
     answer=question();
     if (answer==true){
         correct ++;
     }  else{
        wrong++;}
         String r;
     r= JOptionPane.showInputDialog("Press Y for another question " +
             "and Q to quit");
     yon= r.charAt(0);
    }while(yon == 'y' || yon == 'Y');{
      int allanswers;
      allanswers= correct + wrong;
    JOptionPane.showMessageDialog(null,"You answered" +allanswers+ "questions"+
            "altogether and" +correct+ "questions correctly"); }
  }


     public static char get_choice(){
        String s;
        s= JOptionPane.showInputDialog(
                "Which of the following problems would you like to solve?" +
                "(+) Add" +
                "(-) Substract" +
                "(*) Multiply" +
                "(/) Divide");
        char choice= s.charAt( 0 );
        return choice;}

     public static boolean question(){
    int a= (int)(Math.random()*(10)+1);
    int b=(int)(Math.random()*(10)+1);
   char op;
   int ka; 
   String kidsanswer;
    op = get_choice();
    kidsanswer= JOptionPane.showInputDialog(a+op+b+"=?");
    ka= Integer.parseInt( kidsanswer);
   int compuanswer;
 switch (op){
     default:
         break;
     case '+':  compuanswer= a+b;
        boolean answer;
        if (compuanswer==ka){
          JOptionPane.showMessageDialog(null, "Well Done!");
        return true;} 
  else if (compuanswer!=ka){
            JOptionPane.showMessageDialog(null, "Wrong, sucker!" +
                    "The correct answer is:" + compuanswer);
          return false;} break;

     case '-': compuanswer= a-b;
     if (compuanswer==ka){
          JOptionPane.showMessageDialog(null, "Well Done!");
        return true;}
        else if (compuanswer!=ka){
            JOptionPane.showMessageDialog(null, "Wrong, sucker!" +
                    "The correct answer is:" + compuanswer);
        return false;} break;
     case '*':
        compuanswer= a*b;
         if (compuanswer==ka){
          JOptionPane.showMessageDialog(null, "Well Done!");
        return true;}
        else if (compuanswer!=ka){
            JOptionPane.showMessageDialog(null, "Wrong, sucker!" +
                    "The correct answer is:" + compuanswer);
           return false;      } break;
     case '/':
          compuanswer= a/b;
      if (compuanswer==ka){
          JOptionPane.showMessageDialog(null, "Well Done!");
        return true;}
        else if (compuanswer!=ka){
            JOptionPane.showMessageDialog(null, "Wrong, sucker!" +
                    "The correct answer is:" + compuanswer);
          return false;} break;
          }}


 }

Recommended Answers

All 5 Replies

I have this wierd kink in this program, it keeps telling me that there is a "missing return statement for public static question. i'm kind of new to JAVA and i've tried everything! please can someone help!

"missing return statement" is saying there is a way question() could run without ever coming across a return statement. It's hard to tell where it needs to go in that un-formatted code but seeing as you are using a switch statement you need to have it return something in the default case, or have it return something out of the switch statement. On another note, generally you put the default case at the bottom of all your other cases. It makes sense that way because default is saying, "if none of the other cases work, do this one." How does it know if the other cases work when you put default first?

On another note, generally you put the default case at the bottom of all your other cases.

I tried doing that, no luck though..

public static boolean question(){
	int a= (int)(Math.random()*(10)+1);
	int b=(int)(Math.random()*(10)+1);
	char op;
	int ka;
	String kidsanswer;
	op = get_choice();
	kidsanswer= JOptionPane.showInputDialog(a+op+b+"=?");
	ka= Integer.parseInt( kidsanswer);
	int compuanswer;
	switch (op){
			
		case '+': compuanswer= a+b;
		boolean answer;
		if (compuanswer==ka){
			JOptionPane.showMessageDialog(null, "Well Done!");
			return true;}
		else if (compuanswer!=ka){
			JOptionPane.showMessageDialog(null, "Wrong, sucker!" +
			"The correct answer is:" + compuanswer);
			return false;} 
		break;
			
		case '-': compuanswer= a-b;
		if (compuanswer==ka){
			JOptionPane.showMessageDialog(null, "Well Done!");
			return true;}
		else if (compuanswer!=ka){
			JOptionPane.showMessageDialog(null, "Wrong, sucker!" +
				"The correct answer is:" + compuanswer);
			return false;} 
		break;
			
		case '*':
		compuanswer= a*b;
		if (compuanswer==ka){
			JOptionPane.showMessageDialog(null, "Well Done!");
			return true;}
		else if (compuanswer!=ka){
			JOptionPane.showMessageDialog(null, "Wrong, sucker!" +
				"The correct answer is:" + compuanswer);
			return false; } 
		break;
			
		case '/':
		compuanswer= a/b;
		if (compuanswer==ka){
			JOptionPane.showMessageDialog(null, "Well Done!");
			return true;}
		else if (compuanswer!=ka){
			JOptionPane.showMessageDialog(null, "Wrong, sucker!" +
				"The correct answer is:" + compuanswer);
			return false;} 
		break;
			
		default:
		break;
	}
	return false;
}

Jasimp already gave you answer which you happily overseen, you missing return statement in case user insert something else then "+ - * /", plus I took liberty to move default statement to place where it belongs. My only hope is that you not gone submit this program anywhere as that offensive return statement in case of wrong answer could get you proper smack in your face...

first of all, i've only just begun to learn java. secondly, i didnt understand that i needed to add another return to the main method, though i figured it out about ten minutes after posting the second time. but thanks anyway. (don't worry, i'm not submitting this code anywhere, who would want such a lame ass program?)

lepetitevoddy, use code tags when posting code next time; makes reading the code snippets presented easier to read. Also read the forum rules and announcements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.