问题描述:

I am trying to populate my array with user input; however, I cannot get it to properly work. I am trying to get an int for the user for the number of products they want to enter. Then I want to populate the array with more user input of actual products (Strings) based on the number of products they said they wanted to enter. This is my code:

import java.util.Scanner;

import java.util.ArrayList;

public class Practice {

public static void main(String[] args) {

bannerPrinter();

getNum();

int num = 0;

ArrayList<String> products = productBuilder(num);

boolean productGuess = getOrder(products);

if (productGuess) {

double price = getPrice();

double tax = getTax(price);

double total = getTotal(tax, price);

printTotal(total);

}

else {

System.out.print("Product not found.");

}

}

public static void bannerPrinter() {

System.out.println();

System.out.println("******************************************");

System.out.println("****** Welcome to my eCommerce app! ******");

System.out.println("******************************************");

System.out.println();

}

public static int getNum() {

Scanner scnr = new Scanner(System.in);

int num = 0;

System.out.print("Enter the number of products: ");

num = scnr.nextInt();

return num;

}

public static ArrayList<String> productBuilder(int num) {

Scanner scnr = new Scanner(System.in);

String arrayNames = "";

ArrayList<String> products = new ArrayList();

for (int i = 0; i <= num; i++) {

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

products.add(arrayNames);

}

return products;

}

public static boolean getOrder(ArrayList<String> products) {

Scanner scnr = new Scanner(System.in);

String guess = "";

boolean productName = products.contains(guess);

System.out.print("Enter a product: ");

guess = scnr.nextLine();

if(productName) {

System.out.println();

System.out.print("This product has been found.");

System.out.println();

}

else {

System.out.println();

}

return productName;

}

public static double getPrice() {

double price = 0.0;

price = (int)(Math.random() * 100);

return price;

}

public static double getTax(double price) {

double tax = 0.0;

tax = price * 0.10;

return tax;

}

public static double getTotal(double price, double tax) {

double saleTotal = 0.0;

saleTotal = price + tax;

return saleTotal;

}

public static void printTotal(double saleTotal) {

System.out.println("You total is $" + saleTotal + "0");

}

}

网友答案:

I may not fully understand what you attempt to do but I did see some interesting lines... see my comments

public static ArrayList<String> productBuilder(int num) {

    Scanner scnr = new Scanner(System.in);
    String arrayNames = "";
    ArrayList<String> products = new ArrayList();

    for (int i = 0; i <= num; i++) {
        System.out.print("Enter the products: ");
        // I assume here scnr is supposed to read something
        // and assign it to arrayNames?
        products.add(arrayNames);
    }
    return products;
}

public static boolean getOrder(ArrayList<String> products) {

    Scanner scnr = new Scanner(System.in);
    String guess = "";

    // I assume this line is supposed to be after
    //    guess = scnr.nextLine()?
    boolean productName = products.contains(guess); // very confusing line

    System.out.print("Enter a product: ");
    guess = scnr.nextLine();

    if(productName) {
        System.out.println();
        System.out.print("This product has been found.");
        System.out.println();
    }
    else {
        System.out.println();
    }

    return productName;
}
相关阅读:
Top