【问题标题】:How do I populate a String array using a for loop [duplicate]如何使用 for 循环填充字符串数组 [重复]
【发布时间】:2019-05-27 03:48:16
【问题描述】:

请记住,我是初学者。 我编写了以下代码来尝试填充字符串数组。它运行但不会打印出字符串数组位置的内容。 (同样的事情似乎对双阵列没问题!)我不明白为什么会这样。如果有人可以帮助我解决这个问题,我将非常感激。请参阅下面的 Main Class 的代码和一个名为 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;
    }
 }

【问题讨论】:

    标签: java arrays string for-loop populate


    【解决方案1】:

    问题是 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
    

    希望这会有所帮助!

    【讨论】:

    • 这真的很有帮助。太感谢了! :D
    猜你喜欢
    • 2022-12-10
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    相关资源
    最近更新 更多