基本java,找不到错误
试着为我的编程入门课做这个程序。我无法让它运行,甚至在我添加printwriter之前,它没有错误,仍然无法运行。任何提示(如何修复以及如何让printwriter工作)都将不胜感激基本java,找不到错误,java,printwriter,Java,Printwriter,试着为我的编程入门课做这个程序。我无法让它运行,甚至在我添加printwriter之前,它没有错误,仍然无法运行。任何提示(如何修复以及如何让printwriter工作)都将不胜感激 package pa2; import java.io.PrintWriter; import java.util.Scanner; public class pa2 { public static void main(String[] args) { // TODO Auto-gen
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();
}
您拥有的一大块代码不是main的一部分,因此它永远不会被调用。您必须1)放入main,或2)放入从main调用的函数中 现在你只是把代码挂在空间里,而不是调用它 也许先把这个抛给大家:
private static void foo(){
// stick block of code in here
}
然后从main调用它,如下所示:
public static void main(String [] args){
foo();
}
只是对公认答案的快速补充 您编写的代码实际上是在一个完全有效的“初始化块”中。但是,它通常用于“初始化”变量等。。你可以用它们做什么是有限制的。(生成线程等的问题) 即: 内部的代码在编译时由编译器执行,但它并不用于处理代码生成。只有变量初始化。下面使用“static”关键字的示例应该允许您执行一些代码,但通常不需要
int a=0;
static {
//Your code
a= 3;
// Other stuff
}
更好的计划是按照瑟琳娜小姐说的做。解释初始化块如何工作的链接 只是对公认答案的快速补充 您编写的代码实际上是在一个完全有效的“初始化块”中。但是,它通常用于“初始化”变量等。。你可以用它们做什么是有限制的。(生成线程等的问题) 即: 内部的代码在编译时由编译器执行,但它并不用于处理代码生成。只有变量初始化。下面使用“static”关键字的示例应该允许您执行一些代码,但通常不需要
int a=0;
static {
//Your code
a= 3;
// Other stuff
}
更好的计划是按照瑟琳娜小姐说的做。解释初始化块如何工作的链接 看起来您在开始代码之前先结束了
main
。。然后你会有随机代码四处游荡,而不是方法的一部分。这正是问题所在。我下面的答案应该能帮你解决这个问题如上所述,您肯定需要清理此代码。谢谢大家。显然是新手犯的错误:P!我很感激你的快速反应,我们都去过。我也是一个相对较新的开发人员,一天中我面对手掌的次数非常多;)看起来您在开始代码之前先结束了main
。。然后你会有随机代码四处游荡,而不是方法的一部分。这正是问题所在。我下面的答案应该能帮你解决这个问题如上所述,您肯定需要清理此代码。谢谢大家。显然是新手犯的错误:P!我很感激你的快速反应,我们都去过。我也是一个相对较新的开发人员,一天中我面对手掌的次数非常多;)