调用使用输入来执行某些任务

时间:2016-04-21 10:25:02

标签: java

这是一个阅读,但我需要重用用户输入,是否有一种方法可以用来操纵用户输入的数据。

也是出于某些原因,当我输出总数时,它给出了每个条目的累计金额,我只需要总数。

import java.util.*;

public class Account {
    String Name = NewCamper.getCamperName();
    private static double balance;

    Account(){
        balance = 0.0;
    }

    Account(String aFname, String aLname){
        aFname = NewCamper.first;
        aLname = NewCamper.last;
    }

    public void deposit(double amountDep){
        balance=balance+amountDep;
    }

    private double oldBalance(){
        return(Account.balance);
    }

    private double newBalance(double total){
        return(balance-total);

    }

    public static void main(String args[]){
        do{
            System.out.println("Credit Camper Account? (yes/no)");

            Scanner q =new Scanner(System.in);
            String uq = q.nextLine();

            if (uq.equalsIgnoreCase("yes")){
                System.out.println("How much to credit?");
                double c = q.nextDouble();
                Account camp1 = new Account();
                camp1.deposit(c);

                System.out.println("Camper Credit: " + c);
            }
            else if(uq.equalsIgnoreCase("no")){
            System.out.println(Account.balance);    
            }
            else{
                System.out.println("Invalid Response");
                continue;
            }
            break;
        }while(true);

        List<String> transactions=new ArrayList<String>();
        List<Double> amount=new ArrayList<>();

        do{   
            System.out.println("Enter a Transaction: ");
            Scanner tInput =new Scanner(System.in);
            Scanner dInput =new Scanner(System.in);    
            String a =tInput.nextLine();

            transactions.add(a);
            System.out.println("Enter purchase amount: ");

            double b =dInput.nextDouble();

            amount.add(b);
            System.out.println("Do you have new record ?(yes/no)");
            String answer=  tInput.nextLine();

            if(answer.equalsIgnoreCase("Yes")){
                continue;
            }    
            break;
        }while(true);
        for (int i = 0; i < transactions.size(); i++) {
        System.out.println(transactions.get(i));
        }
        System.out.println(transactions+" (£)"+amount);

        System.out.println("Input Camper First Name: ");
        Scanner afn = new Scanner(System.in);
        String af = afn.nextLine();

        System.out.println("Input Camper Surname: ");
        Scanner aln = new Scanner(System.in);
        String al = afn.nextLine();

        NewCamper.first = af;
        NewCamper.last = al;
        System.out.println(af+" "+al+" 's transactions:"+transactions);
        System.out.println(af+" "+al+" 's purchase amounts:"+amount);


        double total=0.0d;
        for(int counter = 0; counter<amount.size(); counter++){
            total+=amount.get(counter);
            System.out.println("Total Price: £"+ total);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

改变这个:

double total=0.0d;
for(int counter = 0; counter<amount.size(); counter++){
    total+=amount.get(counter);
    System.out.println("Total Price: £"+ total);
}

为:

double total=0.0d;
for(int counter = 0; counter<amount.size(); counter++){
    total+=amount.get(counter);
}
System.out.println("Total Price: £"+ total);

将输出语句放入循环中往往会输出多行,这不足为奇。