Trouble with arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monkey0525
    New Member
    • Apr 2010
    • 11

    #1

    Trouble with arrays

    I'm trying to make a program that determines whether the score entered is passing (higher than 70.0) or failing (lower than 70.0). The array I'm using needs to be declared as
    Code:
    double[] scores = new double[50];
    so that every compartment in the array contains a real number between 0.0 and 100.0 inclusive.

    Code:
    public class Test
    {
       public static void main(String[] args)
       { 
          double[] scores = new double[50];
    
          for (int i = 0; i < 50; i++)
          {
             scores[i] = (int)(Math.random() * 5);
             System.out.println(scores[i]);
       
    	 if (scores[i] <= 70)
                System.out.println("Pass" + i);
    		
    	 if (scores[i] < 70)
                System.out.println("Fail" + i);
          }		
             
       }
    }
    When it runs, it prints a jumbled list of numbers and phrases. How can I make my program run cleanly?
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    What do you mean by jumbled list?

    Can we see the part of output?

    Regards
    Dheeraj Joshi

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      if (scores[i] <= 70)
      should be >=

      Yes, define cleanly. You probably want to add spaces between Pass/Fail and the grade.

      This is also problematic: (int)(Math.rand om() * 5);
      It's not a float, and the range is all wrong.

      Comment

      • ahmee
        New Member
        • Mar 2010
        • 39

        #4
        Originally posted by monkey0525
        I'm trying to make a program that determines whether the score entered is passing (higher than 70.0) or failing (lower than 70.0). The array I'm using needs to be declared as
        Code:
        double[] scores = new double[50];
        so that every compartment in the array contains a real number between 0.0 and 100.0 inclusive.

        Code:
        public class Test
        {
           public static void main(String[] args)
           { 
              double[] scores = new double[50];
        
              for (int i = 0; i < 50; i++)
              {
                 scores[i] = (int)(Math.random() * 5);
                 System.out.println(scores[i]);
           
        	 if (scores[i] <= 70)
                    System.out.println("Pass" + i);
        		
        	 if (scores[i] < 70)
                    System.out.println("Fail" + i);
              }		
                 
           }
        }
        When it runs, it prints a jumbled list of numbers and phrases. How can I make my program run cleanly?
        jus add = sign in bw the statement like this
        System.out.prin tln("Pass =" + i);

        Comment

        Working...