日期:2014-05-20  浏览次数:20614 次

java,菜鸟求救!
import java.util.Scanner;
public class InvoiceTest 
{
public static void main(String args[])
{
Invoice invoice = new Invoice(0);
Scanner put = new Scanner(System.in);
int A;
String str1;
String str2;
double b;

System.out.println("enter:");
str1 = put.nextLine();
invoice.setInvoiceCode(str1);
System.out.println("enter:");
str2 = put.nextLine();
invoice.setInvoiceText(str2);
System.out.println("enter:");
A = put.nextInt();
invoice.setInvoiceNumber(A);
System.out.println("enter:");
b = put.nextDouble();
invoice.setInvoicebalance(b);
System.out.printf("code:%s,text:%s,number:%d,accout:%.2f",str1,str2,A,b,invoice.getInvoiceAmount());
}

}
class Invoice
{
private double balance;
private String invoiceCode;
private String invoiceText;
private int invoiceNumber;
private double invoicebalance;
public Invoice(double balance)
{
int number = 0;
double amount = 0.0;
if(invoicebalance>0.0)
balance = amount*number;
}

public void setInvoiceCode(String code)
{
code = invoiceCode;
}
public void setInvoiceText(String text)
{
text = invoiceText;
}
public void setInvoiceNumber(int number)
{
number = invoiceNumber;
}
public void setInvoicebalance(double amount)
{
amount = invoicebalance;
}


public double getInvoiceAmount()
{
return balance;
}

}
为什么运行后,没有我想要等到的结果。invoice.getInvoiceAmount());没有返回他们的积?

------解决方案--------------------
不是很清楚你想要计算的数值,你看看是下面程序的效果么?
Java code

import java.util.Scanner;
public class InvoiceTest
{
    public static void main(String args[])
    {
        Invoice invoice = new Invoice();
        Scanner put = new Scanner(System.in);
        int A;
        String str1;
        String str2;
        double b;

        System.out.println("enter InvoiceCode:");
        str1 = put.nextLine();
        invoice.setInvoiceCode(str1);
        System.out.println("enter InvoiceText:");
        str2 = put.nextLine();
        invoice.setInvoiceText(str2);
        System.out.println("enter InvoiceNumber:");

        A = put.nextInt();
        invoice.setInvoiceNumber(A);
        System.out.println("enter Invoicebalance:");
        b = put.nextDouble();
        invoice.setInvoicebalance(b);
        System.out.printf("code:%s,text:%s,number:%d,accout:%.2f",str1,str2,A,invoice.getInvoiceAmount());
//        System.out.println(invoice.getInvoiceAmount());
    }


}
class Invoice
{
    private double balance;
    private String invoiceCode;
    private String invoiceText;
    private int invoiceNumber;
    private double invoiceBalance;
    
    public Invoice()
    {
        
    }

    public void setInvoiceCode(String code)
    {
        invoiceCode=code ;
    }
    public void setInvoiceText(String text)
    {
        invoiceText=text;
    }
    public void setInvoiceNumber(int number)
    {
        invoiceNumber=number ;
    }
    public void setInvoicebalance(double amount)
    {
        invoiceBalance=amount;
    }


    public double getInvoiceAmount()
    {
        if(invoiceBalance>0.0)
            balance = invoiceBalance*invoiceNumber;
        return balance;
    }

}

------解决方案--------------------
Java code

import java.util.Scanner;

public class InvoiceTest {
    public static void main(String args[]) {
    Invoice invoice = new Invoice(0);
    Scanner put = new Scanner(System.in);
    int A;

    String str1;
    String str2;
    double b;

    System.out.println("enter:");// invoiceCode
    str1 = put.nextLine();
    if (!str1.matches("[\\d]+")) {
        System.out.println("errot");
        System.exit(0);
    }
    invoice.setInvoiceCode(str1);

    System.out.println("enter:"); // invoiceText
    str2 = put.nextLine();
    if (!str2.matches("[\\d]+")) {
        System.out.println("errot");
        System.exit(0);
    }
    invoice.setInvoiceText(str2);

    System.out.println("enter:");// invoiceNumber
    A = put.nextInt();
    invoice.setInvoiceNumber(A);

    System.out.println("enter:");// invoicebalance
    b = put.nextDouble();
    invoice.setInvoicebalance(b);

    
    
    /*
    System.out.printf("code:%s,text:%s,number:%d,accout:%.2f", str1, str2,
        A, b, invoice.getInvoiceAmount());//为什么这里有5个输出,但是控制格式符只有4个
        */
    
    System.out.printf("code:%s,text:%s,number:%d,accout:%.2f,getinvoiceBalance = %.2f " ,str1, str2,
        A, b, invoice.getInvoiceAmount());
    
    }

}

class Invoice {
    private double balance;
    private String invoiceCode;
    private String invoiceText;
    private int invoiceNumber;
    public double invoiceBalance;

    // ///////////////////////////////////////////////////////////
    public Invoice(double balance) {//这里的构造函数不起作用
    int number = 0;
    double amount = 0.0;
    if (invoiceBalance > 0.0)// 系统默认初始化invoicebalance为0.0
        balance = amount * number;// 这里干什么的???一点作用都没有
    }

    // //////////////////////////////////////////////////////////
    public void updateInvoiceBalance(int amount, double number) {
    if (invoiceBalance > 0.0)
        balance = amount * number;
    }
    // //////////////////////////////////////////////////////////
    public void setInvoiceCode(String code) {
    //code = invoiceCode;// 这里又是干神马的?????
    invoiceCode = code;
    }

    public void setInvoiceText(String text) {
    //text = invoiceText;// 这里又是干神马的?????
    invoiceText = text;
    }

    public void setInvoiceNumber(int number) {
    //number = invoiceNumber;// 这里又是干神马的?????
    invoiceNumber = number;
    }

    public void setInvoicebalance(double amount) {
    //amount = invoiceBalance;// 这里又是干神马的?????
    invoiceBalance = amount*invoiceNumber;
    }

    public double getInvoiceAmount() {//为什么不取名字getInvoiceBalance    
    balance = invoiceBalance;
    //return balance;// 这里又是干神马的?????
    return invoiceBalance;
    }

}