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: Need help with my code (Unreachable code error)

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with my code (Unreachable code error)

    Hi, I recently started programming with java and I was doing a code where two random numbers from 1-10 would be chosen and then a random operator would be chosen as well, the user would have to input the answer to the question and the program is suppose to check if its correct or not.

    import java.util.Random;
    import java.util.Scanner;

    /**
     * Johan Biesert
     * Project Chapter 4
     * Part 1
     * October 30 2018
     */
     
    public class Part_2 {
     
    	public static void main(String[] args) {
     
     
    		//variables
     
    			int First;
    			int Second;
    			int random1;
    			int operator;
    			int calc;
    			int ans;
     
    			First = (int) (( 10 - 1 + 1) *Math.random() + 1);
    			Second = (int) (( 10 - 1 + 1) *Math.random() + 1);
    			random1 = (int) (( 4 - 1 + 1) *Math.random() + 1);
     
    			operator = ' ';
    			switch (operator) { 
    			case 1: operator = '+'; break;
    			case 2: operator = '-'; break;
    			case 3: operator = '*'; break;
    			case 4: operator = '/'; break;
     
    			if (random1 == 1)	   {     [B]Error starts here[/B]
    				operator = 1; }
    			else if (random1 == 2) {
    				operator = 2; }
    			else if (random1 == 3) {
    				operator = 3; }
    			else if (random1 == 4) {
    				operator = 4;
     
    			System.out.println("solve the following");
     
    			System.out.println(First + operator + Second);
    			calc = (First + operator + Second);
     
     
    			Scanner input = new Scanner(System.in);
    			ans = input.nextInt();
    			input.close();
     
    			if (ans == calc) {
    				System.out.println("correct");
    			} else {
    				System.out.println("incorrect");
    				}
     
     
     
     
     
     
     
    				}
     
    			}
    		}
    	}
    Last edited by Prexxy; October 31st, 2018 at 09:41 AM.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Need help with my code (Unreachable code error)

    You have your {} placed incorrectly. You need one right after the switch statement. And delete on at the end of your code.
    But you have some other problems. That is not how you calculate calc. You need to do it in the switch statement for each operator.

    And a couple of other items.

    10-1+1 = 10
    4-1+1 = 4
    etc.

    So get rid of those in your random statements and just use the whole number.

    Finally, look at this code.

    if (random1 == 1)	{
    operator = 1;
    } else if (random1 == 2) {
    operator = 2;
    } else if (random1 == 3) {
    operator = 3;
    } else if (random1 == 4) {
    operator = 4;
    }

    Why not just get rid of random1 and assign the value to operator directly?

    Regards,
    Jim

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,337
    Thanks
    53
    Thanked 2,394 Times in 2,348 Posts

    Default Re: Need help with my code (Unreachable code error)

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: May 8th, 2014, 06:25 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 10:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 09:41 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 08:30 PM
  5. Methods Help - Unreachable statement error
    By jessica202 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 10:24 AM