java - java中的在线购物车解析错误

标签 java

我在学校作业中遇到了实验室问题。我不断收到错误: ShoppingCartPrinter.java:29:错误:解析时到达文件末尾 } ^ 1 个错误

我检查了我的代码,没有发现哪里遗漏了任何大括号。

这是实验室详细信息。

使用类创建一个程序,该程序在下面的 zyLabs 开发人员中执行以下操作。在本实验中,您将使用两个不同的类文件。要切换文件,请在开发人员窗口顶部查找显示“当前文件”的位置。单击当前文件名,然后选择您需要的文件。

(1)创建两个文件提交:

ItemToPurchase.java - 类定义 ShoppingCartPrinter.java - 包含 main() 方法 使用以下规范构建 ItemToPurchase 类:

私有(private)字段

  • String itemName - 在默认构造函数中初始化为“none”
  • int itemPrice - 在默认构造函数中初始化为 0
  • int itemQuantity - 在默认构造函数中初始化为 0

默认构造函数

公共(public)成员方法(修改器和访问器)

  • setName() 和 getName()(2 分)
  • setPrice() 和 getPrice()(2 分)
  • setQuantity() 和 getQuantity()(2 分)

(2) 在 main() 中,提示用户输入两个项目并创建 ItemToPurchase 类的两个对象。在提示输入第二项之前,调用 scnr.nextLine();允许用户输入新字符串。 (2分)

例如: 项目 1 输入商品名称: 巧克力片 输入商品价格:3 输入商品数量:1

项目 2 输入商品名称:瓶装水 输入商品价格:1 输入商品数量:10 (3) 将两项成本相加,输出总成本。 (2分)

例如:

总成本 巧克力片 1 @ $3 = $3 瓶装水 10 @ $1 = $10

总计:13 美元

这是我的代码:

购物车打印机

import java.until.Scanner; 

public class ShoppingCartPrinter{
   //main method
   public static void main(String[] args){
      //create object of scanner
      Scanner scnr = new Scanner(System.in);
      //variable declaration
      int i = 0;
      String productName;
      int productPrice = 0;
      int productQuantity = 0;
      int cartTotal = 0;
      
      ItemToPurchase item1 = new ItemToPurchase();
      ItemToPurchase item2 = new ItemToPurchase();
      
      //get item 1 details
      System.out.println("Item 1");
      
      System.out.println("Enter the item name: ");
      productName = scnr.nextLine();
      item1.setName(productName);
      
      System.out.println("Enter the item price: ");
      productPrice = scnr.nextInt();
      item1.setPrice(productPrice);
      scnr.nextLine();
      
      System.out.println("Enter the item quantity: ");
      productQuantity = scnr.nextInt();
      item1.setQuantity(productQuantity);
      scnr.nextLine();
      
      scnr.nextLine();
      System.out.println("");
      
      //get item 2 details
      System.out.println("Item 2");
      
      System.out.println("Enter the item name: ");
      productName = scnr.nextLine();
      item2.setName(productName);
      
      System.out.println("Enter the item price: ");
      productPrice = scnr.nextInt();
      item2.setPrice(productPrice);
      scnr.nextLine();
      
      System.out.println("Enter the item quantity: ");
      productQuantity = scnr.nextInt();
      item2.setQuantity(productQuantity);
      scnr.nextLine();
      
      System.out.println("");
      
      //add cost of item 1 and 2 and print total
      cartTotal = (item1.getQuantity() * item1.getPrice()) + (item2.getQuantity() * item2.getPrice());
      System.out.println("TOTAL COST");
      //cart total is item 1 price * quantity + item 2 price * quantity
      //item 1 info
      int item1Total = item1.getPrice() * item1.getQuantity();
      System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + item1Total);
      
      //item 2 info
      int item2Total = item2.getPrice() * item2.getQuantity();
      System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + item2Total);
   
      //output total
      System.out.println("");
      System.out.print("Total: $" + cartTotal);
      
   }
}

要购买的商品

public class ItemToPurchase {
   //Private fields - itemName, itemPrice, and itemQuanity
   private String itemName;
   private int itemPrice;
   private int itemQuantity;
   //Default Constructor
   public ItemToPurchase(){
      itemName = "none";
      itemPrice = 0;
      itemQuantity = 0;
   }       
   //public member methods
   public void setName(String itemName){
      this.itemName = itemName;
   }
   public String getName(){
      return itemName;
   }
   public void setPrice(int itemPrice){
      this.itemPrice = itemPrice;
   }
   public int getPrice(){
      return itemPrice;
   }
   public void setQuantity(int itemQuantity){
      this.itemQuantity = itemQuantity;
   }
   public int getQuantity(){
      return itemQuantity;
   }
   
   public void printItemPurchase() {
      System.out.println(itemQuantity + " " + itemName + " $" + itemPrice +  
                         " = $" + (itemPrice * itemQuantity));
   }
}

最佳答案

问题 1:您的问题是下一个 int 不考虑进入下一次读取的输入中的换行符。因此名称返回为空白。

问题 2:您正在将 String 分配给 int。引用:https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-

以下代码对我来说效果很好: ShoppingCartPrinter.java

  import java.util.Scanner;
  
  public class ShoppingCartPrinter{
     //main method
     public static void main(String[] args){
        //create object of scanner
        Scanner scnr = new Scanner(System.in);
        //variable declaration
        int i = 0;
        String productName;
        int productPrice = 0;
        int productQuantity = 0;
        int cartTotal = 0;
        
        ItemToPurchase item1 = new ItemToPurchase();
        ItemToPurchase item2 = new ItemToPurchase();
        
        //get item 1 details
        System.out.println("Item 1");
        
        System.out.println("Enter the item name: ");
        productName = scnr.nextLine();
        item1.setName(productName);
        
        System.out.println("Enter the item price: ");
        productPrice = scnr.nextInt();
        item1.setPrice(productPrice);
        
        System.out.println("Enter the item quantity: ");
        productQuantity = scnr.nextInt();
        item1.setQuantity(productQuantity);
        
        scnr.nextLine();
        System.out.println("");
        
        //get item 2 details
        System.out.println("Item 2");
        
        System.out.println("Enter the item name: ");
        productName = scnr.nextLine();
        item2.setName(productName);
        
        System.out.println("Enter the item price: ");
        productPrice = scnr.nextInt();
        item2.setPrice(productPrice);
        
        System.out.println("Enter the item quantity: ");
        productQuantity = scnr.nextInt();
        item2.setQuantity(productQuantity);
        
        System.out.println("");
        
        //add cost of item 1 and 2 and print total
        cartTotal = (item1.getQuantity() * item1.getPrice()) + (item2.getQuantity() * item2.getPrice());
        System.out.println("TOTAL COST");
        //cart total is item 1 price * quantity + item 2 price * quantity
        //item 1 info
        int item1Total = item1.getPrice() * item1.getQuantity();
        System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + item1Total);
        
        //item 2 info
        int item2Total = item2.getPrice() * item2.getQuantity();
        System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + item2Total);
     
        //output total
        System.out.println("");
        System.out.print("Total: $" + cartTotal);
        
     }
  }

ItemsToPruchase.java

    public class ItemToPurchase {
        //Private fields - itemName, itemPrice, and itemQuanity
        private String itemName;
        private int itemPrice;
        private int itemQuantity;
    
        //Default Constructor
        public ItemToPurchase() {
            itemName = "none";
            itemPrice = 0;
            itemQuantity = 0;
        }
    
        //public member methods
        public void setName(String itemName) {
            this.itemName = itemName;
        }
    
        public String getName() {
            return itemName;
        }
    
        public void setPrice(int itemPrice) {
            this.itemPrice = itemPrice;
        }
    
        public int getPrice() {
            return itemPrice;
        }
    
        public void setQuantity(int itemQuantity) {
            this.itemQuantity = itemQuantity;
        }
    
        public int getQuantity() {
            return itemQuantity;
        }
    
        public void printItemPurchase() {
            System.out.println(itemQuantity + " " + itemName + " $" + itemPrice +
                    " = $" + (itemPrice * itemQuantity));
        }
    }

输出:

Item 1
Enter the item name: 
item 1
Enter the item price: 
20
Enter the item quantity: 
5

Item 2
Enter the item name: 
item 2
Enter the item price: 
2
Enter the item quantity: 
10

TOTAL COST
item 1 5 @ $20 = $100
item 2 10 @ $2 = $20

Total: $120

你放错了括号。一旦我修好它就可以了。请确保您使用 IDE 而不是 zybooks

import java.util.Scanner;

public class ShoppingCartPrinter {
   //main method
   public static void main(String[] args) {
      //create object of scanner
      Scanner scnr = new Scanner(System.in);
      //variable declaration
      int i = 0;
      String productName;
      int productPrice = 0;
      int productQuantity = 0;
      int cartTotal = 0;

      ItemToPurchase item1 = new ItemToPurchase();
      ItemToPurchase item2 = new ItemToPurchase();

      //get item 1 details
      System.out.println("Item 1");

      System.out.println("Enter the item name: ");
      productName = scnr.nextLine();
      item1.setName(productName);

      System.out.println("Enter the item price: ");
      productPrice = scnr.nextInt();
      item1.setPrice(productPrice);

      System.out.println("Enter the item quantity: ");
      productQuantity = scnr.nextInt();
      item1.setQuantity(productQuantity);

      scnr.nextLine();
      System.out.println("");

      //get item 2 details
      System.out.println("Item 2");

      System.out.println("Enter the item name: ");
      productName = scnr.nextLine();
      item2.setName(productName);

      System.out.println("Enter the item price: ");
      productPrice = scnr.nextInt();
      item2.setPrice(productPrice);

      System.out.println("Enter the item quantity: ");
      productQuantity = scnr.nextInt();
      item2.setQuantity(productQuantity);

      System.out.println("");

      //add cost of item 1 and 2 and print total
      cartTotal = (item1.getQuantity() * item1.getPrice()) + (item2.getQuantity() * item2.getPrice());
      System.out.println("TOTAL COST");
      //cart total is item 1 price * quantity + item 2 price * quantity
      //item 1 info
      int item1Total = item1.getPrice() * item1.getQuantity();
      System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + item1Total);

      //item 2 info
      int item2Total = item2.getPrice() * item2.getQuantity();
      System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + item2Total);

      //output total
      System.out.println("");
      System.out.print("Total: $" + cartTotal);

   }

   public static class ItemToPurchase {
      //Private fields - itemName, itemPrice, and itemQuanity
      private String itemName;
      private int itemPrice;
      private int itemQuantity;

      //Default Constructor
      public ItemToPurchase() {
         itemName = "none";
         itemPrice = 0;
         itemQuantity = 0;
      }

      //public member methods
      public void setName(String itemName) {
         this.itemName = itemName;
      }

      public String getName() {
         return itemName;
      }

      public void setPrice(int itemPrice) {
         this.itemPrice = itemPrice;
      }

      public int getPrice() {
         return itemPrice;
      }

      public void setQuantity(int itemQuantity) {
         this.itemQuantity = itemQuantity;
      }

      public int getQuantity() {
         return itemQuantity;
      }

      public void printItemPurchase() {
         System.out.println(itemQuantity + " " + itemName + " $" + itemPrice +
                 " = $" + (itemPrice * itemQuantity));
      }
   }
}

关于java - java中的在线购物车解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77181663/

相关文章:

java - 递归方法寻找最小整数?

Java - 如何为 JButton 应用 3 个键的键盘快捷键?

Java JUnit 5 AssertAll 流

java - 如何使用 Java 8 流将字符串数组转换为大整数数组

java - 使用 RecyclerView 动态绑定(bind)简单待办事项列表中的列表项

java - native 方法链接错误

java - RxJava : call function when two observables with different result types are completed

java - 无法将 csv 文件导入 JAVA

java - 将多个字符串混合成所有可能的组合

java - 通过 Jsoup 使用 IP 和主机