gpt4 book ai didi

java - 如何将 Java 打印到窗口

转载 作者:太空宇宙 更新时间:2023-11-04 12:14:12 25 4
gpt4 key购买 nike

我编写一些代码只是为了好玩并帮助我熟悉流程。我对 Java 编程非常陌生,所以请耐心等待。

目前,我只是在 Notepad++ 中编写并在命令提示符中进行编译,但我想了解如何编译并将输出打印到窗口,而不是将其打印到命令提示符中。

public class SalesCalculations {
public static void main (String args[]){
//This section is shirt sales calculations

int shirtCost;
shirtCost = 12;

int shirtSales;
shirtSales = 200;

int shirtSalePrice;
shirtSalePrice = 25;

int shirtMakeCost;
shirtMakeCost = shirtCost * shirtSales;

int shirtSaleTotal;
shirtSaleTotal = shirtSalePrice * shirtSales;

int shirtProfit;
shirtProfit = shirtSaleTotal - shirtMakeCost;

System.out.println("");
System.out.println("Cost to make Shirt: " + "$" + shirtCost);
System.out.println("Sales Price of Shirt: " + "$" + shirtSalePrice);
System.out.println("Number of Shirt sales: " + shirtSales);
System.out.println("This months Shirt making price: " + "$" + shirtMakeCost);
System.out.println("Shirt sales money this month: " + "$" + shirtSaleTotal);
System.out.println("Profit of Shirts: " + "$" + shirtProfit);
System.out.println("");

}
}

任何帮助或指导将不胜感激!

最佳答案

如果还不清楚,请使用 .java 创建文件进行编译,转到命令提示符并运行

javac filename.java

您创建一个具有 .class 扩展名的新文件,该文件将在没有扩展名的情况下使用命令执行

java filename

您可以使用JOptionPane类在对话框中打印结果,考虑到 "\n" 将导致换行,例如在其代码中为

JOptionPane.showMessageDialog(null,"Cost to make Shirt:           " + "$" +  shirtCost + "\n "
+ "Sales Price of Shirt: " + "$" + shirtSalePrice + " \n "
+ "Number of Shirt sales: " + shirtSales + "\n"
+ "This months Shirt making price: " + "$" + shirtMakeCost + "\n"
+ "Shirt sales money this month: " + "$" + shirtSaleTotal + "\n"
+ "Profit of Shirts: " + "$" + shirtProfit);

要工作必须导入类

import javax.swing.JOptionPane;

对于数据图形类,它提供了输入方法,例如

int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Input Value "));

remember that this method returns a string so it is necessary to convert it to an integer

关于java - 如何将 Java 打印到窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39565505/

25 4 0
文章推荐: c++ - 组合和循环依赖
文章推荐: html -
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com