2016-11-08 68 views
-1

我只有几个小时练习和学习Java,所以我仍然在学习基础知识。在java中以字符串格式将所有值相乘在一起

我从一个文本文件,其中包含读取值:

单 112.50

主 2227.50

阁楼 5000.00

(所以它显示为运行时) 房间类型:单人,预订:6 0,房间价格:£112.00,收入:£6,750.00,税:1350.00 所以第四个房间。

我已经打印了需要的字符串格式的所有值。不过,我的问题很简单。

我只是想将所有收入加在总收入变量中,并将所有paidTax一起添加到totalpaidTax变量中,然后继续打印出来,基本显示所有房间的总税收和总收入。 虽然,我只是不知道如何写它。我尝试过多次尝试,但没有运气。 这是我目前的代码。

import java.io.FileReader; 
import java.util.Scanner; 
public class WagesCalculator { 
public static void main(String[] args) throws Exception { 
    Scanner input = new Scanner(System.in); 
    Scanner file = new Scanner(new FileReader("task3.txt")); 
    Scanner sc = new Scanner(System.in); 

    //Current tax variable value 
    double tax = 20; 

    //User Input Y or N to change tax variable value 
    System.out.println("- - Hotel Tax System - -"); 
    System.out.print("Do you want to specify a custom Tax Rate? [Y|N]: "); 

    //if statement to change tax variable value subject to Y or N 
    if (sc.next().equalsIgnoreCase("Y")) { 
     System.out.print("Please enter the new tax value: "); 
     tax = new Scanner(System.in).nextInt(); 
     } 

    //Prints out current tax value 
    System.out.println("The current tax rate is " + tax+"."); 

    while (file.hasNext()) { 
     String name = file.next(); 
     int numberOfBookings = file.nextInt(); 
     double price = file.nextDouble(); 
     double income = numberOfBookings * price; 
     double paidTax = income*(tax/100); 
     //String format print out final calculations 
     System.out.printf("Room Type: %s, Bookings: %d, Room Price: £%.2f, Income: £%.2f, Tax: %.2f %n", name, numberOfBookings, price, income, paidTax); 

     } 
    file.close(); 
    } 

}

+1

什么似乎是准确的问题?如果您希望获得帮助,至少您必须隔离存在问题的部分代码。 – MarsAtomic

+1

你能展示一个尝试并描述问题吗? “[能力]添加到以下代码:”是不是真的适合SO http://stackoverflow.com/help/how-to-ask – Taylor

+1

我只是问你怎么可以多个存储在收入的价值在一起,到一个新变量,并将所有来自paidTax的值添加到一个新变量中。 我刚刚发布了代码,所以没有人与这个问题混淆。 没有问题。我只是要求将所有值加在一起的语法提供帮助。你们两个似乎真的在窗外吹来了一个非常简单的问题。 – Luke

回答

0

对象是你的朋友。

  • 在输入中为每个房间创建一个对象。
  • 将房间存储在列表中。
  • 来自列表的聚合值。
  • 相应打印。

    import java.io.FileNotFoundException; 
    import java.io.FileReader; 
    import java.util.ArrayList; 
    import java.util.List; 
    import java.util.Scanner; 
    
    public class WagesCalculator 
    { 
    
        public static void main(String[] args) 
        throws Exception 
        { 
        WagesCalculator wc = new WagesCalculator(); 
        wc.calculate(); 
        } 
    
        public void calculate() 
        throws FileNotFoundException 
        { 
        Scanner file = new Scanner(new FileReader("task3.txt")); 
        Scanner sc = new Scanner(System.in); 
    
        // Current tax variable value 
        double tax = 20; 
    
        // User Input Y or N to change tax variable value 
        System.out.println("- - Hotel Tax System - -"); 
        System.out.print("Do you want to specify a custom Tax Rate? [Y|N]: "); 
    
        // if statement to change tax variable value subject to Y or N 
        if (sc.next().equalsIgnoreCase("Y")) 
        { 
         System.out.print("Please enter the new tax value: "); 
         tax = new Scanner(System.in).nextInt(); 
        } 
    
        // Prints out current tax value 
        System.out.println("The current tax rate is " + tax + "."); 
    
        List<Room> rooms = new ArrayList<Room>(); 
    
        while (file.hasNext()) 
        { 
         String name = file.next(); 
         int numberOfBookings = file.nextInt(); 
         double price = file.nextDouble(); 
         rooms.add(new Room(tax, name, numberOfBookings, price)); 
        } 
    
        file.close(); 
    
        rooms.stream().forEach(e -> System.out.println(e)); 
        double totalIncome = rooms.stream().map(r -> r.income) 
        .reduce((a, b) -> a + b).orElse(0.0); 
        double totalTax = rooms.stream().map(r -> r.tax).reduce((a, b) -> a + b) 
        .orElse(0.0); 
        System.out.printf("Total income was: %d\nTotal tax was %d\n", totalIncome, 
        totalTax); 
        } 
    
        class Room 
        { 
        double tax; 
    
        String name; 
    
        int numberOfBookings; 
    
        double price; 
    
        double income; 
    
        double paidTax; 
    
        public Room(double tax, String name, int numberOfBookings, double price) 
        { 
         this.tax = tax; 
         this.name = name; 
         this.numberOfBookings = numberOfBookings; 
         this.price = price; 
         this.income = numberOfBookings * price; 
         this.paidTax = income * (tax/100); 
        } 
    
        @Override 
        public String toString() 
        { 
         return String.format(
         "Room Type: %s, Bookings: %d, Room Price: £%.2f, Income: £%.2f,   Tax: %.2f %n", 
          name, numberOfBookings, price, income, paidTax); 
        } 
        } 
    }