如何使用for循环填充String数组

时间:2018-12-29 21:22:29

标签: java arrays string for-loop populate

请记住,我是初学者。 我编写了以下代码来尝试填充String数组。它会运行,但不会打印出String数组位置的内容。 (对于双数组,同一件事似乎没有问题!)我不明白为什么会这样。如果有人能帮助我解决这个问题,我将非常感谢。请参见下面的Main类代码和一个名为Fruit的类,其中包含我正在使用的方法。

`enter code here`Main class:
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Fruit fruit = new Fruit();

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of items in your shopping list:");
        int listSize = scanner.nextInt();
        String[] sl = new String[listSize];
        double[] price = new double[listSize];

        //Loop asking user to enter items and prices
        for(int i = 0; i <= listSize - 1; i++)
        {
            System.out.println("Enter item " + (i+1) + ":");
            fruit.setName(scanner.nextLine());
            sl[i] = fruit.getName();
            scanner.next();

            System.out.print("Price of " + sl[i] + ":");
            fruit.setPrice(scanner.nextDouble());
            price[i] = fruit.getPrice();
         }
         //Loop printing items and their prices
         for(int i = 0; i <= listSize - 1; i++)
         {
             System.out.println(sl[i] + " cost " + price[i]);

         }
         //Order the array in ascending order so as to be able to easily 
identify the most and least expensive
        Arrays.sort(price);
        //Print out the most expensive and cheapest prices
        System.out.println("The most expensive item costs: " + 
price[price.length-1]);
        System.out.println("The least expensive item costs: " + price[0]);

    }
}

Fruit Class:

public class Fruit {
    private String name;
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
         this.price = price;
    }
 }

1 个答案:

答案 0 :(得分:0)

问题是按Enter键后,Scanner.readInt()和Scanner.readDouble()方法不会读取换行符。但是,readLine()方法读取跳过的输入,因此它读取换行符。若要解决此问题,您需要在调用scanner.readInt()和scanner.readDouble()之后调用scanner.readLine()来摆脱换行符。请参见下面的代码。

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Fruit fruit = new Fruit();

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of items in your shopping list:");
        int listSize = scanner.nextInt();
        scanner.nextLine(); //calling nextLine() to get rid of the newline character
        String[] sl = new String[listSize];
        double[] price = new double[listSize];

        //Loop asking user to enter items and prices
        for(int i = 0; i <= listSize - 1; i++)
        {
            System.out.println("Enter item " + (i+1) + ":");
            fruit.setName(scanner.nextLine());
            sl[i] = fruit.getName();

            System.out.print("Price of " + sl[i] + ":");
            fruit.setPrice(scanner.nextDouble());
            scanner.nextLine(); //calling nextLine() to get rid of the newline character
            price[i] = fruit.getPrice();
         }
         //Loop printing items and their prices
         for(int i = 0; i <= listSize - 1; i++)
         {
             System.out.println(sl[i] + " cost " + price[i]);

         }
         //Order the array in ascending order so as to be able to easily 
        Arrays.sort(price);
        //Print out the most expensive and cheapest prices
        System.out.println("The most expensive item costs: " + price[price.length-1]);
        System.out.println("The least expensive item costs: " + price[0]);

    }
}

class Fruit 
{
    private String name;
    private double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
         this.price = price;
    }
 }

我已经编译并运行了这段代码,这是它产生的输出:

Enter the number of items in your shopping list:2
Enter item 1:
apple
Price of apple:1.25
Enter item 2:
bread
Price of bread:3.45
apple cost 1.25
bread cost 3.45
The most expensive item costs: 3.45
The least expensive item costs: 1.25

希望这会有所帮助!