gpt4 book ai didi

java - JOptionPane,JTextArea,在JScrollPane中,显示输出

转载 作者:行者123 更新时间:2023-12-02 14:10:18 24 4
gpt4 key购买 nike

这是给类(class)的硬件作业,我相信我一切正常,但我无法在输出中显示我需要的所有信息。如果您查看 TestCashier.java,您可以看到我的预期输出。我需要知道如何制作 generateReceipt()average()makeChange()tendered() > 也显示在 Pane 上。任何帮助将不胜感激。我已经在这部分上停留了很长一段时间了。

如果有人愿意采取额外的步骤来帮助我解决我的 add() 问题,并找出为什么我在向用户询问每件商品的价格时无法使其正常工作。我需要使用 add() 来计算总计,但只有当我手动将它们添加在一起并将该值分配给 totalSum 时,我的程序才能正常工作。感谢您花时间阅读本文,感谢您的所有意见/反馈。 (甚至批评...我是来学习的)

收银员.java

package cashier;
import java.text.NumberFormat;

public class Cashier {
static int numItems;
static double totalSum,averagePrice,price;
private int pennies,nickles, dimes, quarters, dollars;
private double tendered,change;

public void Cashier(){
this.numItems = 0;
this.totalSum = 0;
}

public void average(){
averagePrice = totalSum/numItems;
NumberFormat nf1 = NumberFormat.getCurrencyInstance();
System.out.println("The average price per item is "+ nf1.format(averagePrice));//Must format

}
public void add(String name, Double price) {
numItems++;
//totalSum =+price;
}


public void tendered(double t) {
this.tendered = t;
this.change = tendered - totalSum;
NumberFormat df1 = NumberFormat.getCurrencyInstance();
System.out.println("Amount tendered is " + df1.format(tendered));
}

void makeChange(Cashier c){

change = (tendered-totalSum);
change =change*100;
NumberFormat df = NumberFormat.getCurrencyInstance();
System.out.println("The change is: " + df.format(change/100)+"\n");//Must format


dollars = (int)(change/100);
change %= 100;


quarters = (int) (change/25);
change %=25;


dimes = (int) (change/10);
change = change%10;


nickles = (int) (change/5);
change = change%5;

pennies = (int)change;

System.out.println("The change includes...");
System.out.println(dollars+" dollars");
System.out.println(quarters+" quarters");
System.out.println(dimes+" dimes");
System.out.println(nickles+" nickles");
System.out.println(pennies+" pennies");

}






}

获取数据.java

package cashier;


import javax.swing.JOptionPane;
import java.text.NumberFormat;


public class GetData {

static double getDouble(String c){
return Double.parseDouble(getWord(c));
}

static String getWord(String c){
return JOptionPane.showInputDialog(c);
}


}

TestCashier.java

package cashier;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;



import static cashier.Cashier.totalSum;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;

public class TestCashier{

public static void main(String[]arg){
NumberFormat nf = NumberFormat.getCurrencyInstance();
Cashier c = new Cashier();

String name = GetData.getWord(" Enter name of first item");
double price = GetData.getDouble("Enter price of item");
c.add(name,price);


String name2 = GetData.getWord(" Enter name of second item");
double price2 = GetData.getDouble("Enter price of item");
c.add(name2,price2);

String name3 = GetData.getWord(" Enter name of third item");
Double price3 = GetData.getDouble("Enter price of item");
c.add(name3,price3);

String name4 = GetData.getWord(" Enter name of fourth item");
Double price4 = GetData.getDouble("Enter price of item");
c.add(name4,price4);

totalSum = price+price2+price3+price4;



//make payment
double tendered = GetData.getDouble("Enter amount of money for payment");



generateReceipt(c);
NumberFormat nf1 = NumberFormat.getCurrencyInstance();
String s = (name+"\t\t"+nf1.format(price));
s = s +("\n"+name2+"\t\t"+nf1.format(price2));
s = s +("\n"+name3+"\t\t"+nf1.format(price3));
s = s +("\n"+name4+"\t\t"+nf1.format(price4));
s = s +("\n"+"_______________________________");
s = s +("\n"+"Total:\t\t"+nf1.format(totalSum));


s = s +("\n\n"+"The number of items purchased is "+Cashier.numItems+" item(s)");


c.tendered(tendered);
c.makeChange(c);

JTextArea text = new JTextArea(s,30,30);
JScrollPane pane = new JScrollPane(text);
JOptionPane.showMessageDialog(null, pane,"THE RIP-OFF STORE",JOptionPane.PLAIN_MESSAGE);



}
static void generateReceipt(Cashier c){

Date current = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
System.out.println("WELCOME TO THE RIP-OFF STORE!");
System.out.println("Home of the 'not so good' deals...");
System.out.println("Thank you for stopping by on "+ df.format(current));
System.out.println("");


}
}

最佳答案

您需要更改函数以连接到字符串并返回该字符串。在您的函数generateReceipt()中,像这样编辑:

static String generateReceipt(Cashier c){
String s = "";
Date current = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
s += ("WELCOME TO THE RIP-OFF STORE!\n");
s += ("Home of the 'not so good' deals...\n");
s += ("Thank you for stopping by on "+ df.format(current)) + "\n\n";
return s;
}

要从该函数获取信息,只需像这样调用它:

字符串 s =generateReceipt(c);

这同样适用于您需要从 Cashier.java 类输出的所有函数。我不会向您展示每个函数,因为每个函数的想法都是相同的。请记住,它将像这样被调用:

s += c.average();

这是 TestCashier 中的所有字符串连接以及我如何获得它:

    String s = generateReceipt(c);
NumberFormat nf1 = NumberFormat.getCurrencyInstance();
s += (name+"\t\t"+nf1.format(price));
s = s +("\n"+name2+"\t\t"+nf1.format(price2));
s = s +("\n"+name3+"\t\t"+nf1.format(price3));
s = s +("\n"+name4+"\t\t"+nf1.format(price4));
s = s +("\n"+"_______________________________");
s = s +("\n"+"Total:\t\t"+nf1.format(totalSum));


s = s +("\n\n"+"The number of items purchased is "+Cashier.numItems+" item(s)");

s += c.average();
s += c.tendered(tendered);
s += c.makeChange(c);

JTextArea text = new JTextArea(s,30,30);
JScrollPane pane = new JScrollPane(text);
JOptionPane.showMessageDialog(null, pane,"THE RIP-OFF STORE",JOptionPane.PLAIN_MESSAGE);

我还没有看过你的 add() 函数,但我也很高兴。如果您需要任何帮助,请与我聊天。

**这里有一个 imgur 链接,指向我的 IDE (IntelliJ Idea) 上的输出: /image/hXEu9.jpg

编辑:

您几乎已经正确添加了功能。在 Cashier.java 中,您的 add() 注释掉了一条语句:

totalSum =+price;只需切换运算符totalSum +=price;

创建像 getTotalSum() 这样的 getter 或类似的东西,因为您总是希望使用 getter 和 setter,而不是直接访问类变量(至少在大多数情况下)。在 TestCashier.java 中调用它,如 totalSum = c.getTotalSum(); 并且它应该可以正常工作。请注意如何将 name 作为 add() 函数的参数。这是为什么?如果您不需要或使用它,请将其废弃。

关于java - JOptionPane,JTextArea,在JScrollPane中,显示输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31063769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com