jeudi 10 janvier 2019

Random String script not working with Scanner

I have been trying to get this script to work just for some review but nothing I do seems to get it past a certain point. It is where it says to push "r" for a random number. When I push "r" nothing happens like I am not activating the methods or I have written them wrong. Here is the code:

package temper2;

public class temp2 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    TempTheRest o = new TempTheRest();
    }

}



package temper2;
import java.util.Random;
import java.util.Scanner;
public class TempTheRest {
public String number1;

public TempTheRest()
    {
    Scanner s = new Scanner(System.in);
    System.out.println("Pick a number 1 - 9");
    int number = s.nextInt();

    System.out.println(number + " is a good number but your choosing     privileges have been revoked ");
    System.out.println("We will use a random number");
    System.out.println("Press 'r' for a random number ");

    number1 = s.next();

    if(number1 == "r")
        {
        //randomMethod2();
        randomMethod();

        System.out.println(randomMethod());
        }
        }

    public static String randomMethod2()
    {
    String rv = "";
    String string1 = randomMethod();
    rv = rv + string1;
    return rv;
    }

    public static String randomMethod()
    {
    String rv = "";
    Random r = new Random();
    for(int i = 0; i >= 9; i++)
        {
        int number1 = r.nextInt(8) + 1;

        if(number1 > 0 && number1 <= 3)
        {
            rv = "Your number is " + number1 + " and fell in the first third";
        } else if(number1 >= 4 && number1 <= 6)
        {
            rv = "Your number is " + number1 + " and fell in the second third";
        } else if( number1 >= 7 && number1 <= 9)
            rv = "Your number is " + number1 + " and fell in the last third";
        }
    return rv;
    }

}

All help is greatly appreciated. Thank You.




Aucun commentaire:

Enregistrer un commentaire