dimanche 12 février 2017

Random Integer Error in Java

Newbie here, I am making a program that produces a random ordinal number with the proper suffix
(I.E. 1st, 2nd, 3rd...) I can not get randomInt(); to work. I keep getting the following errors:

    .\Random.java:6: error: class RandomInteger is public, should be declared in a  
file named RandomInteger.java  
public final class RandomInteger {  
             ^  
RandomNumSuffix.java:8: error: cannot access Random  
    Random rand = new Random();  
    ^  
  bad source file: .\Random.java  
    file does not contain class Random  
    Please remove or make sure it appears in the correct subdirectory of the  
sourcepath.

I have searched on Google, and Stack Overflow to find a solution to no avail. I have even copied
and compiled other programs from the internet that use randomInt(); and they all produce the same error. Can you tell me what I am doing wrong? Here is the code for my program:

import java.util.*;

class RandNumSuffix
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);
        Random rand  = new Random();
        String numSuffix = "";
        String answer = "";
        String repeat = "";
        int x;

        while(repeat.equalsIgnoreCase("yes")||repeat.equalsIgnoreCase("y" ))
        {
            x = rand.nextInt(1000000)+1;
            if (x == 1)
            {
                numSuffix = "st";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x == 2)
            {
                numSuffix = "nd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x == 3)
            {
                numSuffix = "rd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x > 20 && x < 101 && x%10==1)
            {
                numSuffix = "st";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x > 20 && x < 101 && x%10==2)
            {
                numSuffix = "nd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x > 20 && x < 101 && x%10==3)
            {
                numSuffix = "rd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x>100 && x%10==1 && x%100!=11)
            {
                numSuffix = "st";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x>100 && x%10==2 && x%100!=12)
            {
                numSuffix = "nd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            if (x>100 && x%10==3 && x%100!=13)
            {
                numSuffix = "rd";
                answer    = x + numSuffix + ", ";
                System.out.print(answer);
            }
            else
            {
                if (answer == "")
                {
                    numSuffix = "th";
                    System.out.print(x + numSuffix + ", ");
                }
            }


        answer = "";
        System.out.println("Would you like to generate another number?");
        answer = scan.nextLine();
        }
    }
} 

Also criticism on my program is welcome. Thanks.




Aucun commentaire:

Enregistrer un commentaire