-3

the purpose of this program is to create an array[n=scan] , insert n numbers and return the max number. Unfortunately the second part doesnt work and i cant figure out why. The score variable works in the first class and its public so it could be use reused, right? Any help? Im kinda a noobie so have mercy !

 //script per creare array [n] ins da tastiera e far scrivere i suoi elementi 
import java.util.*;
public class es2{
    public static void main(String[] args){
        // Read size of array and declare array
        System.out.println("Enter number of elements:");
        Scanner scan = new Scanner(System.in);
        int size = scan.nextInt();
        double[] score = new double[size];
        // read elements and store in array
        for (int k = 0; k < score.length; k++)
        {
            System.out.println("Enter element " + k);
            score[k] = scan.nextDouble();
        }
    }
    public int max(){
        //create var max
        int max = 0;
        for (int i = 1; i < score.length; i++)
        {if (score[i] > score[max])
                max = i;
        }   
                System.out.println("il max e' "+score[max]);

    }
}
caiusp
  • 1
  • 3
  • 4
    Where are you calling your `max()` method? – Abubakkar Jun 28 '17 at 15:06
  • 1
    score is a local variable. see: https://stackoverflow.com/questions/20671008/what-is-the-difference-between-a-local-variable-an-instance-field-an-input-par or http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – OH GOD SPIDERS Jun 28 '17 at 15:07
  • *"The score variable works in the first class and its public so it could be use reused"* - huh? It's inside the same class but a different method. Which means it's not `public` it's local to the method – UnholySheep Jun 28 '17 at 15:07
  • 1
    Looks like a scope issue. "score" is accessible in the main function but not in max as it was declared in the main function. Try passing it into max or declaring public double[] score outside of main. – Michael Warner Jun 28 '17 at 15:09

3 Answers3

1

Pass your array in your static method max() in this way :

public class es2 {

  public static void main(String[] args){

    System.out.println("Enter number of elements:");
    Scanner scan = new Scanner(System.in);
    int size = scan.nextInt();
    double score[] = new double[size];
    for (int k = 0; k < score.length; k++)
    {
        System.out.println("Enter element " + k);
        score[k] = scan.nextDouble();
    }
    max(score);
  }

  public static void max(double[] score){

    int max = 0;
    for (int i = 1; i < score.length; i++){
        if (score[i] > score[max])
            max = i;
    }   
    System.out.println("il max e' "+score[max]);

   }
}

EDIT

Want to return max value, do small modification :

public static int max(double[] score){

    int max = 0;
    for (int i = 1; i < score.length; i++){
        if (score[i] > score[max])
            max = i;
    }   
    System.out.println("il max e' "+score[max]);
    return (int)score[max];  
}
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
0

You need to change the scope of score and also add either a return value to max or make the method void. The max method could not see the variable "score" because of its scope.

import java.util.*;

public class es2{

    private double[] score;
    private Scanner scan = new Scanner(System.in);

    public static void main(String[] args){
        es2 main = new es2();
        main.load_values();
        main.max();
    }

  public void load_values(){
        System.out.println("Enter number of elements:");
        int size = scan.nextInt();
        score = new double[size];
        // read elements and store in array
        for (int k = 0; k < score.length; k++)
        {
            System.out.println("Enter element " + k);
            score[k] = scan.nextDouble();
        }
  }

    public void max(){
        //create var max
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < score.length; i++)
        {
          if (score[i] > max)
              max = score[i];
        }   
        System.out.println("il max e' "+max);

    }
}
Ryan
  • 1,972
  • 2
  • 23
  • 36
  • thanks for your reply. im concerned about your last statement because, reading from the professor's slides, this "max" method should work with negative numbers too! – caiusp Jun 28 '17 at 15:32
-1

`public class ArrayMax {

public static void main(String[] args){
    // Read size of array and declare array
    System.out.println("Enter number of elements:");
    Scanner scan = new Scanner(System.in);
    int size = scan.nextInt();
    double[] score = new double[size];
    // read elements and store in array
    for (int k = 0; k < score.length; k++)
    {
        System.out.println("Enter element " + k);
        score[k] = scan.nextDouble();
    }
    max(score);
}

public static int max(double[] score) {
    // create var max
    int max = 0;
    for (int i = 1; i < score.length; i++) {
        if (score[i] > score[max])
            max = i;
    }
    System.out.println("max element is' " + score[max]);
    return (int) score[max];
}

} `

Anand Sinha
  • 322
  • 3
  • 7
  • thanks for your reply! this programs works! may i ask you why you wrote "return (int) score[max];"? because the program already print the max value even without your last sentence. – caiusp Jun 28 '17 at 15:39