问题描述:

I'm trying to create a method that asks the user for the number of names and values they want, then prompts the user to enter those names and values. Then it's suppose to return the first name of the person with the lowest value and print in main. Error keeps popping up that it cannot find the variable "name". Here's my code so far

public static void main(String[] args){

String name = minValue();

System.out.println(name);

}//end main

public static String minValue(){

Scanner input = new Scanner(System.in);

System.out.print("How many first names are there?");

int numOfNames = input.nextInt();

for(int number = 0; number < numOfNames; number++){

System.out.print("Enter name: ");

String name = input.next();

}

System.out.print("How many values are there?");

int numOfValues = input.nextInt();

for (int count = 0; count < numOfValues; count++){

System.out.print("Enter the value: ");

double value = input.nextDouble();

}

return name;

}//end minValue

I also believe I'm not doing something right with connecting the values to the name. Really confused with this.

网友答案:

Error keeps popping up that it cannot find the variable "name".

Move name out of the for loop:

String name = null;
for (int number = 0; number < numOfNames; number++) {
    System.out.print("Enter name: ");
    name = input.next();
}

Explanation:

The for loop's braces ({ and }) define a scope in which the variables are defined. Since you defined name inside that block, it will only be available inside it. By moving the declaration outside of the block to the scope of return name, it will be available for referencing.

It's suppose to return the first name of the person with the lowest value and print in main.

If you want to remember the names and values you read you have to put them all in some data structure (an array, a list, a map etc.). I assume that you're trying to use the most basic structures, so work with arrays. Also, it makes no sense to have a different number of names and values in your case. Try to understand and complete this code:

public static String minValue() {

    Scanner input = new Scanner(System.in);

    System.out.print("How many first names are there?");
    int numOfNames = input.nextInt();
    String[] names = new String[numOfNames];
    double[] values = new double[numOfNames];
    for (int number = 0; number < numOfNames; number++) {
        System.out.print("Enter name: ");
        names[number] = input.next();
        System.out.print("Enter the value: ");
        values[number] = input.nextDouble();
    }

    int i = 0;
    // write a way to get the index i of the smallest value in values

    return names[i];
}
网友答案:

Use this piece of code instead! It helps you to store values in an array and write the code to minimum value in the space provided!

Scanner input = new Scanner(System.in);
        System.out.print("How many first names are there?");
        int numOfNames = input.nextInt();
        String name[]=new String[numOfNames];
        for (int number = 0; number < numOfNames; number++) {
            System.out.print("Enter name: ");
             name[number] = input.next();
        }
        System.out.print("How many values are there?");
        int numOfValues = input.nextInt();
        double value[]=new double[numOfValues];
        for (int count = 0; count < numOfValues; count++) {
            System.out.print("Enter the value: ");
            value[count] = input.nextDouble();
        }
        /* Write function to calculate min value here */
        return min value;
网友答案:

In the method minValue, you defined the String name variable inside the first for loop, and then you are trying to access it outside the loop scope. You should do something like this:

public static String minValue(){

  ...
  String name = "";
  for( ... ) {
    name = input.next();
  }

  ...

  return name;
}
网友答案:

You are doing return name at the end of your method but you don't declare it anywhere in the right scope. You've defined it within main (which means it is only available in that method) and in the for loop (which means it's re-created for each loop iteration and only available there).

You can add String name = "" at the start of your minValue method as a quick fix, but I suggest you read up on variable scope so you can learn why your code is incorrect.

网友答案:

This should return name to you. What you did is define name inside the for loop. this means as soon as a for loop run is done the name variable is discarded. Define variables you wish to return or use outside of loops outside a loop(like at the beginning of the method. This should work to return name to you.

public static void main(String[] args){
  String name = minValue();
  System.out.println(name);
}//end main

public static String minValue(){
  String name="none";
  Scanner input = new Scanner(System.in);
  System.out.print("How many first names are there?");
  int numOfNames = input.nextInt();
for(int number = 0; number < numOfNames; number++){
  System.out.print("Enter name: ");
  name = input.next();
}
  System.out.print("How many values are there?");
  int numOfValues = input.nextInt();
for (int count = 0; count < numOfValues; count++){
   System.out.print("Enter the value: ");
   double value = input.nextDouble();
 }
 return name;
}//end minValue 

Also this piece of code does entirely nothing with the double values...

  System.out.print("How many values are there?");
  int numOfValues = input.nextInt();
for (int count = 0; count < numOfValues; count++){
   System.out.print("Enter the value: ");
   double value = input.nextDouble();
 }
相关阅读:
Top