Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: case 3

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default case 3

    How do I start a new case after the given code? Do I start a new method?
    import javax.swing.*;
     
    import java.awt.*;
     
    public class CplusPlus extends java.applet.Applet {
     
     
    	String statement = "a single set of commands";
    	String comment = "a line or portion of a line that is ignored by the C++ compiler";
    	String declaration = " a statement that defines a variable";
    	String variable = " a holding place in the computer's memory" +
    			" to store a value of some type";
    	String expression = " a command that has a value";
    	String operator = " a command that generates a value";
     
    	String vocabulary = "a statement is " + statement + "\na comment is " + comment + 
    	"\na declaration is " + declaration + "\na variable is " + variable + "\nan expression is " + 
    	expression + "an operator is " + operator;
    	TextArea letter;
     
     
    	public void init(){
     
    		String chapters = JOptionPane.showInputDialog("chapter1\n" + "chapter2\n"
    				+ "chapter3\n" + "chapter4\n");
    		int choice = Integer.parseInt(chapters);
    		switch(choice){
    		case 1:		
    		String statement = "a single set of commands";
    		String comment = "a line or portion of a line that is ignored by the C++ compiler";
    		String declaration = " a statement that defines a variable";
    		String variable = " a holding place in the computer's memory" +
    				" to store a value of some type";
    		String expression = " a command that has a value";
    		String operator = " a command that generates a value";
     
    		//TextArea initializing
     
    		String letter = "Learn this mom. I made this for you! Study well because a test will follow. " + "\n\n\na statement is " + 
    		statement + "\na comment is " + comment + "\na declaration is " + declaration +
    		"\na variable is " + variable + "\nan expression is " + expression + "\nan operator is " + operator + "\n\n\nan " +
    				"expression example is int x = 3;" + "\na declaration example is JTextArea outputArea; " + "\na comment example is // initializing" +
    				"\na variable example is output" + "\nanother variable example is x " + "\na statement example is System.out.println();" +
    				"\nan operator example is = " + "\nanother operator example is - " + "\nanother operator example is + ";
    		;
    		TextArea let;
    	let = new TextArea(letter,40,90);
    	add(let);
    	JOptionPane.showMessageDialog(null,let);
     
    		//Quiz
     
    		JOptionPane.showMessageDialog(null,"what is a statement");
    		String answer = JOptionPane.showInputDialog("A - " + statement + "\n" +
    				"B - " + comment + "\n" + "C - "  + 
    				declaration + "\n" + "D - " + variable + "\n" + "E - " + expression
    				 + "\n" + "F - " + operator);
    		if(answer.equals("A"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
     
    		//introducing the new question
     
    		JOptionPane.showMessageDialog(null,"what is a comment");
    		String answer2 = JOptionPane.showInputDialog("A - " + statement + "\n" +
    				"B - " + comment + "\n" + "C - "  + 
    				declaration + "\n" + "D - " + variable + "\n" + "E - " + expression
    				 + "\n" + "F - " + operator);
    		if(answer2.equals("B"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
     
    		//introducing the new question
     
    		JOptionPane.showMessageDialog(null,"what is a variable");
    		String answer3 = JOptionPane.showInputDialog("A - " + statement + "\n" +
    				"B - " + comment + "\n" + "C - "  + 
    				declaration + "\n" + "D - " + variable + "\n" + "E - " + expression
    				 + "\n" + "F - " + operator);
    		if(answer3.equals("D"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
     
    //introducing the new question
     
    		JOptionPane.showMessageDialog(null,"what is a declaration");
    		String answer4 = JOptionPane.showInputDialog("A - " + statement + "\n" +
    				"B - " + comment + "\n" + "C - "  + 
    				declaration + "\n" + "D - " + variable + "\n" + "E - " + expression
    				 + "\n" + "F - " + operator);
    		if(answer4.equals("C"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
    //introducing the new question
     
    		JOptionPane.showMessageDialog(null,"what is an operator");
    		String answer5 = JOptionPane.showInputDialog("A - " + statement + "\n" +
    				"B - " + comment + "\n" + "C - "  + 
    				declaration + "\n" + "D - " + variable + "\n" + "E - " + expression
    				 + "\n" + "F - " + operator);
    		if(answer5.equals("F"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
    //introducing the new question
     
    		JOptionPane.showMessageDialog(null,"what is an expression");
    		String answer6 = JOptionPane.showInputDialog("A - " + statement + "\n" +
    				"B - " + comment + "\n" + "C - "  + 
    				declaration + "\n" + "D - " + variable + "\n" + "E - " + expression
    				 + "\n" + "F - " + operator);
    		if(answer6.equals("E"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
     
    		// example questions
     
    		JOptionPane.showMessageDialog(null,"what is an example of a statement?");
    		String example = ("int x = 3;");
    		String example2 = ("JTextArea outputArea;");
    		String example3 = (" // initializing");
    		String example4 = ("output");
    		String example5 = ("System.out.println();");
    		String option = JOptionPane.showInputDialog("A - " + example + "\n" + "B - " + example2 +
    				"\n" + "C - " + example3 + "\n" + "D - " + example4 + "\n" + "E - " + example5 + "\n");
    		if(option.equals("E"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
     
    // examples
     
    		JOptionPane.showMessageDialog(null,"what is an example of a comment?");
     
    		String option2 = JOptionPane.showInputDialog("A - " + example5 + "\n" + "B - " + example2 +
    				"\n" + "C - " + example3 + "\n" + "D - " + example4 + "\n" + "E - " + example);
    		if(option2.equals("C"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
     
    // examples
     
    		JOptionPane.showMessageDialog(null,"what is an example of a declaration?");
     
    		String option3 = JOptionPane.showInputDialog("A - " + example5 + "\n" + "B - " + example2 +
    				"\n" + "C - " + example3 + "\n" + "D - " + example4 + "\n" + "E - " + example);
    		if(option3.equals("B"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
    // examples
     
    		JOptionPane.showMessageDialog(null,"what is an example of a variable?");
     
    		String option4 = JOptionPane.showInputDialog("A - " + example5 + "\n" + "B - " + example2 +
    				"\n" + "C - " + example3 + "\n" + "D - " + example4 + "\n" + "E - " + example);
    		if(option4.equals("D"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
    // examples
     
    		JOptionPane.showMessageDialog(null,"what is an example of an expression?");
     
    		String option5 = JOptionPane.showInputDialog("A - " + example5 + "\n" + "B - " + example2 +
    				"\n" + "C - " + example3 + "\n" + "D - " + example4 + "\n" + "E - " + example);
    		if(option5.equals("E"))
    			JOptionPane.showMessageDialog(null,"correct");
    		else
    			JOptionPane.showMessageDialog(null,"incorrect");
    		JOptionPane.showMessageDialog(null,"Congratulations! you made it past chapter 1");
    break;
    		case 2:
    			String frame = ""; 
    			TextArea lt;
    			Button lesson1 = new Button("lesson 1");
    			Button exit = new Button("exit");
    			Button lesson2 = new Button("lesson 2");
    			Button lesson3 = new Button("lesson 3");
    			Button lesson4 = new Button("lesson 4");
    			Button lesson5 = new Button("lesson 5");
    			Button lesson6 = new Button("lesson 6");
    			Button lesson7 = new Button("lesson 7");
    			Button lesson8 = new Button("lesson 8");
    			Button lesson9 = new Button ("lesson 9");
    			Button lesson10 = new Button("lesson 10");
    			String output =("memorize this frame as your lesson 1, " +
    					"which is the start of all c++ programs") +
    			 ("\n\n\n #include <iostream>\n") + ("#include <cstdio>") + ("\n#include <cstdlib>")
    			 + ("\n using namespace std;") + ("\nint main(int nNumberofArgs, char* pczArgs[]){");
    			JOptionPane.showConfirmDialog(null, output);
    			JOptionPane.showMessageDialog(null,"now you try");			
    		lt = new TextArea(frame,20,80);
     
    		add(lt);
    		add(lesson1);
    		add(exit);	
    		add(lesson2);
    		add(lesson3);
    		add(lesson4);
    		add(lesson5);
    		add(lesson6);
    		add(lesson7);
    		add(lesson8);
    		add(lesson9);
    		add(lesson10);
    		String output2 	= ("Now learn how to close a program!");
    		System.out.println(output2);
     
    	}
    	}
    		// button action event to guide the user through every lesson
    	public boolean action(Event evt, Object arg){
     
    		String output = ("memorize this frame and write it, which is the start of all c++ programs") +
    		 ("\n\n\n #include <iostream>\n") + ("#include <cstdio>") + ("\n#include <cstdlib>")
    		 + ("\n using namespace std;") + 
    		 ("\nint main(int nNumberofArgs, char* pczArgs[ ]){");	
    		String output2 = "Now learn how to end a program \n\nLearn this, memorize it, now write it!" 
    			+ "\n\n\nSystem(pause);" + "\nreturn 0;";
    		String output3 = "A computer needs to know what kind of variable the user is using to process" +
    		"\nthe program. The kind of variables are expressed by type. In other words, a type is just an identification marker." +
    		"\nTherefore, the most common type for words is String. So, initilizing a string " +
    		"\nwould look like String sEric.\n\nnow you try initializing your name.";
    		String output4 = "The most common type for numbers is double because double can represent fractions and whole numbers." +
    				" \nSo, here is an exampleof a double:\n\ndouble 2.5. \nnow you try writing a double. Type int is used for integers, example:" +
    				"\n\nint 2";
    		String output5 = "A less common type is character. This is used for single letters like a,b,c, etc. \n Example is: \n\nchar d.";
    		String output6 = "When you tell the computer to do a command, it will show as something like this: \n\ncout<< type your name;" + 
    		"\n\nor int nAge; \n//(declaration) \n\ncin>> int nAge; \n//(this is where the user inputs his age) " +
    		"\n\ncout<< Eric is nAge; \n//(statement) + result is: \n//31" + "\n\nnow you try telling the computer to output your age or weight or height";
    		String output7 = "When you tell a user to input a command, it looks like:" + "\nint nAge;" +
    				" \ncin>> int nAge;\n\n//(this is where the user inputs his age and stays in the computer's memory)" + "\nnow you try telling a user"
    				+ "to input his grade point average";
    		String output8 = "now write the frame and tell the computer to output that I love my mom and write the end frame. \nIt should look like this:" +
    		"\n\n#include<iostream>\n#include<cstdlib>\n#include<cstdio>\nusing namespace std;\nint main(int nNumberofArgs, char* pczArgs[]{" + 
    		"\ncout<<I love my mom;\nSystem(pause);\nreturn 0;\n}";
    		String output9 = "a brace ({) must always be closed at some point(}). a statement must always end with a semicolon(;). ";
    		String output10 = "congratulations! you are finished with chapter 2!" + "\n I am very proud of you!";
     
    		if (evt.target instanceof Button){
    			String label = (String)arg;
    			if ( label.equals("lesson 1"))
    			JOptionPane.showMessageDialog(null,output);
     
     
    		else{
    				if(label.equals("exit"))
    					System.exit(0);		
     
    		else	{
     
    		if(label.equals("lesson 2"))
     
    			JOptionPane.showMessageDialog(null,output2);			
     
    		else{
    		if(label.equals("lesson 3"))
    					JOptionPane.showMessageDialog(null,output3);
    		else{
    			if(label.equals("lesson 4"))
    				JOptionPane.showMessageDialog(null,output4);
    			else{
    				if(label.equals("lesson 5"))
    					JOptionPane.showMessageDialog(null,output5);
    				else{
    					if(label.equals("lesson 6"))
    						JOptionPane.showMessageDialog(null,output6);
    					else{
    						if(label.equals("lesson 7"))
    							JOptionPane.showMessageDialog(null,output7);
    						else{
    							if(label.equals("lesson 8"))
    								JOptionPane.showMessageDialog(null,output8);
    							else{
    								if(label.equals("lesson 9"))
    									JOptionPane.showMessageDialog(null,output9);
    								else{
    									if(label.equals("lesson 10"))
    										JOptionPane.showMessageDialog(null,output10);
    		}
    		}
    		}
    		}
    		}
    		}
    		}
    	}
    		}
    		}
    		}
    	return true;	
    			}	
     
    public void finish(){
    }
    case 3:
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: case 3


  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: case 3

    The formatting of your code make it Very Hard to see the logic. There should be indentations to show nesting.
    Original:
    		switch(choice){
    		case 1:		
    		String statement = "a single set of commands";
    Indented to show nested logic:
    		switch(choice){
    		   case 1:		
    		       String statement = "a single set of commands";
                           ...
                        case 2:
                            ....
                     }
    Having all the statements start in the same column makes it hard to understand the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 12:10 AM
  2. Please help me with my Case Study
    By dalexquisite in forum Object Oriented Programming
    Replies: 2
    Last Post: September 26th, 2012, 11:58 PM
  3. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM
  4. orphaned case
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:35 PM
  5. [SOLVED] upper case
    By andaji in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2010, 11:54 PM