gpt4 book ai didi

java - 基础java,找不到错误

转载 作者:行者123 更新时间:2023-12-01 18:43:10 25 4
gpt4 key购买 nike

尝试为我的编程入门类(class)编写这个程序。我无法让它运行,即使在添加打印机之前它也没有错误并且仍然无法运行。任何提示将不胜感激(如何修复它以及如何让打印机工作。

package pa2;

import java.io.PrintWriter;
import java.util.Scanner;

public class pa2 {

public static void main(String[] args) {
// TODO Auto-generated method stub

}

{

Scanner keyboard = new Scanner(System.in);

PrintWriter prw = new PrintWriter ("pa2output.txt");

// variables
double tshirt, chips, coke, tax, sale, subtotal, total, tshirtcost, chipscost, cokecost, deposit, cokewdeposit, discount, change, payment, numshirt, numcoke, numchips;
String name;
tshirt = 18.95;
chips = 1.79;
coke = 2.99;
deposit = 1.2;
tax = .06;
sale = .15;

// menu
System.out.print("What is your name? ");
prw.print("What is your name? ");
name = keyboard.nextLine();
System.out.println("Welcome to Denny's " + name + "! " + "We have the following items for sale:");
prw.print("Welcome to Denny's " + name + "! " + "We have the following items for sale:");
System.out.println("T-shirt $" + tshirt + "15% off");
prw.print("T-shirt $" + tshirt + "15% off");
System.out.println("Chips $" + chips + "15% off");
prw.print("Chips $" + chips + "15% off");
System.out.println("Coke $" + coke);
prw.print("Coke $" + coke);

// input
System.out.println("How many T-shirts do you want?");
prw.print("How many T-shirts do you want?");
numshirt = keyboard.nextDouble();
System.out.println("How many bags of patato chips?");
prw.print("How many bags of patato chips?");
numchips = keyboard.nextDouble();
System.out.println("What about 12-pack Coke?");
prw.print("What about 12-pack Coke?");
numcoke = keyboard.nextDouble();
System.out.println("Please enter your payment: ");
prw.print("Please enter your payment: ");
payment = keyboard.nextDouble();
// variables

cokewdeposit = (deposit * numcoke);
tshirtcost = (tshirt * numshirt);
chipscost = (chips * numchips);
cokecost = (coke * numcoke);
subtotal = (tshirtcost + chipscost + cokecost + cokewdeposit);
discount = (sale * subtotal);
total = subtotal + (subtotal * tax) - discount;
change = payment - total;

// calculation
System.out.println("Your total is $" + total);
prw.print("Your total is $" + total);
System.out.println(name + "here is your receipt");
prw.print(name + "here is your receipt");
System.out.println("item," + " " + "unit price," + " " + "how many," + " " + "cost");
prw.print("item," + " " + "unit price," + " " + "how many," + " " + "cost");
System.out.println("T-shirt ");
prw.print("T-shirt ");
System.out.print(tshirt + " " + numshirt + " " + (tshirt * numshirt));
prw.print(tshirt + " " + numshirt + " " + (tshirt * numshirt));
System.out.println("Chips ");
prw.print("Chips ");
System.out.print(chips + " " + numchips + " " + (chips * numchips));
prw.print(chips + " " + numchips + " " + (chips * numchips));
System.out.println("Coke" + coke);
prw.print("Coke" + coke);
System.out.print(coke + " " + numcoke + " " + (coke * numcoke));
prw.print(coke + " " + numcoke + " " + (coke * numcoke));

// receipt
System.out.println("Discount " + subtotal);
prw.print("Discount " + subtotal);
System.out.println("Discount " + discount);
prw.print("Discount " + discount);
System.out.println("Tax " + tax);
prw.print("Tax " + tax);
System.out.println("Total " + total);
prw.print("Total " + total);
System.out.println("Payment " + payment);
prw.print("Payment " + payment);
System.out.println("Your change is " + change);
prw.print("Your change is " + change);
System.out.println("Thank you. Come again!");
prw.print("Thank you. Come again!");


keyboard.close();
prw.close();
}
}

最佳答案

您拥有的大代码块不是 main 的一部分,因此永远不会被调用。您必须 1) 放入 main,或 2) 放入从 main 调用的函数中。

现在,您只有代码卡在太空中,而您没有调用它。

对于初学者来说,也许可以将其扔掉:

private static void foo(){ 
// stick block of code in here
}

然后从 main 中调用它,如下所示:

 public static void main(String [] args){
foo();
}

关于java - 基础java,找不到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19039787/

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