RandomInt and If Else issue

I'm new to Java and I have tried repeatedly to fix this... I have thirty errors

* @(#)Input.java
*
* Input application
*
* @author
* @version 1.00 2015/6/24
*/

import javax.swing.JOptionPane;
import java.util.Random;

public class Input {

    public static void main(String[] args) {

        // TODO, add your application code

        String name = JOptionPane.showInputDialog("Rock, Paper, or Scissors?");
        System.out.println(name+"!");
        Random rn = new Random();
        random.nextInt(10 - 1 + 1) + 1;
}


        if (name.equalsIgnoreCase("Rock")){
            if (0<randomInt && randomInt<4){
                System.out.println("Paper! You Lose!");
        }
            else if(4<= randomInt &&randomInt <=6){
                System.out.println("Rock! We Tied!");
            }
                else{
                System.out.println("Scissors! You Win!");
            }
    }
    if (name.equalsIgnoreCase("Paper")){
                    if (0<randomInt && randomInt<4){
            System.out.println("Scissors! You Lose!");
        }
            else if(4<= randomInt &&randomInt <=6){
                System.out.println("Paper! We Tied!");
            }
                else{
                System.out.println("Rock! You Win!");
            }
    }
    }
    if (name.equalsIgnoreCase("Scissors")){
                    if (0<randomInt && randomInt<4){
            System.out.println("Rock! You Lose!");
        }
            else if(4<= randomInt &&randomInt <=6){
                System.out.println("Scissors! We Tied!");
            }
                else{
                System.out.println("Paper! You Win!");
            }
    }
    }

When it is ran:

--------------------Configuration: Input - JDK version 1.7.0_05 <Default> - <Default>--------------------
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:22: error: not a statement
        random.nextInt(10 - 1 + 1) + 1;
                                   ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: illegal start of type
        if (name.equalsIgnoreCase("Rock")){
        ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error:    <identifier> expected
            if (name.equalsIgnoreCase("Rock")){
                                 ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: ';' expected
        if (name.equalsIgnoreCase("Rock")){
                                  ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: illegal start of type
        if (name.equalsIgnoreCase("Rock")){
                                        ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: <identifier> expected
        if (name.equalsIgnoreCase("Rock")){
                                         ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:26: error: ';' expected
        if (name.equalsIgnoreCase("Rock")){
                                          ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: illegal start of type
            if (0<randomInt && randomInt<4){
            ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: <identifier> expected
            if (0<randomInt && randomInt<4){
              ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: ';' expected
            if (0<randomInt && randomInt<4){
                ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: > expected
            if (0<randomInt && randomInt<4){
                           ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: illegal start of type
            if (0<randomInt && randomInt<4){
                            ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: '(' expected
            if (0<randomInt && randomInt<4){
                                        ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: <identifier> expected
            if (0<randomInt && randomInt<4){
                                         ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: illegal start of type
            if (0<randomInt && randomInt<4){
                                          ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:27: error: <identifier> expected
            if (0<randomInt && randomInt<4){
                                           ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: '(' expected
                System.out.println("Paper! You Lose!");
                ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: illegal start of type
                System.out.println("Paper! You Lose!");
                      ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: ';' expected
                System.out.println("Paper! You Lose!");
                          ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: invalid method declaration; return type required
                System.out.println("Paper! You Lose!");
                           ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:28: error: illegal start of type
                System.out.println("Paper! You Lose!");
                                   ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:30: error: class, interface, or enum expected
                else if(4<= randomInt &&randomInt <=6){
                ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:32: error: class, interface, or enum expected
                }
                ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:35: error: class, interface, or enum expected
                }
                ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:40: error: class, interface, or enum expected
            }
            ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:43: error: class, interface, or enum expected
                }
                ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:46: error: class, interface, or enum expected
                }
                ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:52: error: class, interface, or enum expected
            }
            ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:55: error: class, interface, or enum expected
                }
                ^
    C:\Users\gw\Documents\JCreator LE\MyProjects\Input\src\Input.java:58: error: class, interface, or enum expected
                }
                ^
30 errors

Process completed.

Upvotes: 2

Views: 88

Answers (5)

Michael
Michael

Reputation: 2773

Isn't random.nextInt(10 - 1 + 1) + 1 just an expression? Put it in the context of something bigger... like an assignment operation

Upvotes: 1

Abhijeet Dhumal
Abhijeet Dhumal

Reputation: 1809

You have not defined randomInt variable. As well as closing braces are not matching up correctly. That is creating these errors.

Please find below code, which will work correctly:

import javax.swing.JOptionPane; import java.util.Random;

public class Input {

    public static void main(String[] args) {

        // TODO, add your application code

        String name = JOptionPane.showInputDialog("Rock, Paper, or Scissors?");
        System.out.println(name+"!");
        Random rn = new Random();
       int randomInt=  rn.nextInt(10)+1;



        if (name.equalsIgnoreCase("Rock")){
            if (0<randomInt && randomInt<4){
                System.out.println("Paper! You Lose!");
        }
            else if(4<= randomInt &&randomInt <=6){
                System.out.println("Rock! We Tied!");
            }
                else{
                System.out.println("Scissors! You Win!");
            }
    }
    if (name.equalsIgnoreCase("Paper")){
                    if (0<randomInt && randomInt<4){
            System.out.println("Scissors! You Lose!");
        }
            else if(4<= randomInt &&randomInt <=6){
                System.out.println("Paper! We Tied!");
            }
                else{
                System.out.println("Rock! You Win!");
            }
    }

    if (name.equalsIgnoreCase("Scissors")){
                    if (0<randomInt && randomInt<4){
            System.out.println("Rock! You Lose!");
        }
            else if(4<= randomInt &&randomInt <=6){
                System.out.println("Scissors! We Tied!");
            }
                else{
                System.out.println("Paper! You Win!");
            }
    }
    }
}

Upvotes: 1

maskacovnik
maskacovnik

Reputation: 3084

As @Marcin said, randomInt is not defined:

Random rn = new Random();
random.nextInt(10 - 1 + 1) + 1;

And random is not defined too, it should look like:

Random rn = new Random();
int randomInt = rn.nextInt(10) + 1;

Wondering why - 1 + 1

Upvotes: 0

Rajesh
Rajesh

Reputation: 2155

Not sure how you got such messed up code.

PFB working code with all corrections:

import javax.swing.JOptionPane;
import java.util.Random;

public class Input {

    public static void main(String[] args) {

        String name = JOptionPane.showInputDialog("Rock, Paper, or Scissors?");
        System.out.println(name + "!");
        Random random = new Random();
        int randomInt = random.nextInt(10 - 1 + 1) + 1;

        if (name.equalsIgnoreCase("Rock")) {
            if (0 < randomInt && randomInt < 4) {
                System.out.println("Paper! You Lose!");
            } else if (4 <= randomInt && randomInt <= 6) {
                System.out.println("Rock! We Tied!");
            } else {
                System.out.println("Scissors! You Win!");
            }
        }
        if (name.equalsIgnoreCase("Paper")) {
            if (0 < randomInt && randomInt < 4) {
                System.out.println("Scissors! You Lose!");
            } else if (4 <= randomInt && randomInt <= 6) {
                System.out.println("Paper! We Tied!");
            } else {
                System.out.println("Rock! You Win!");
            }
        }

        if (name.equalsIgnoreCase("Scissors")) {
            if (0 < randomInt && randomInt < 4) {
                System.out.println("Rock! You Lose!");
            } else if (4 <= randomInt && randomInt <= 6) {
                System.out.println("Scissors! We Tied!");
            } else {
                System.out.println("Paper! You Win!");
            }
        }
    }
}

Upvotes: 1

SwiftySwift
SwiftySwift

Reputation: 477

The first problem is you're using randomInt in all your statements, but you defined rn.

So the compiler has no idea what randomInt is.

Edit: Please fix that and post any more errors.

Another problem is that you closed off your main function right under the random.nextInt(10 - ....) statement.

Upvotes: 1

Related Questions