我的编程课程需要一些帮助。这是一个递归程序,它采用用户给出的小计和小费率,输出全部总费用和小费用。这是我到目前为止所得到的,由于某种原因它只是不起作用:
import java.io.*;
import java.until.Scanner;
public class gratuity {
private double total;
private double subTotal;
private double gratRate;
private double newSubTotal;
private double newGratRate;
public static void main(String[] args) {
System.out.print("Enter the subtotal: ");
System.out.print("Enter the gratuity rate: ");
Scanner scan = new Scanner(System.in);
Scanner myScan = new Scanner(System.in);
double subtotal = scan.nextDouble();
double gratRate = myScan.nextDouble();
System.out.println("The Gratuity is: " + newSubtotal);
System.out.println("The Total is: " + Total);
}
public static double computeGratRate() {
double newGratRate = (gratRate/100);
return newGratRAte;
}
public static double computeNewSub() {
double newSubTotal - (subTotal * newGratRate);
return newSubTotal;
}
public static double computeTotal() {
double total = (newSubTotal + newGratRate);
return total;
}
}
如果有人帮我弄清楚如何修复它,我将非常感激!谢谢!
答案 0 :(得分:2)
一些事情。
您正在Main中创建名为“subtotal”和“gratRate”的新变量。这些值会覆盖类的成员变量。
无论如何你的问题都不会编译,因为......
所有方法都是静态的,这没关系,但这些静态方法正在访问非静态变量。将此类的所有成员变量设为静态。 (或者使Main之外的所有内容都不是静态的,然后将“Main”作为存根来创建小费类的实例。
您需要导入java.util.Scanner
,而不是java.until.Scanner
。
此行是编译器错误:
double newSubTotal - (subTotal * newGratRate);
我认为你的意思是:
double newSubTotal = (subTotal * newGratRate);
现在应该提供足够的提示....继续尝试。
答案 1 :(得分:0)
你的意思是:
import java.util.Scanner;
public class Gratuity {
private double subTotal;
private double gratRate;
public static void main(String[] args) {
Gratuity gratuity = new Gratuity();
System.out.print("Enter the subtotal: ");
Scanner scan = new Scanner(System.in);
gratuity.setSubTotal(scan.nextDouble());
System.out.print("Enter the gratuity rate: ");
Scanner myScan = new Scanner(System.in);
gratuity.setGratRate(myScan.nextDouble());
System.out.println("The new GratRate is: " + gratuity.getNewGratRate());
System.out.println("The New Sub is: " + gratuity.getNewSub());
System.out.println("The Total is: " + gratuity.getTotal());
}
public double getNewGratRate() {
return gratRate/100;
}
public double getNewSub() {
return getNewGratRate() * subTotal;
}
public double getTotal() {
return getNewSub() + getNewGratRate();
}
public double getSubTotal() {
return subTotal;
}
public void setSubTotal(double subTotal) {
this.subTotal = subTotal;
}
public double getGratRate() {
return gratRate;
}
public void setGratRate(double gratRate) {
this.gratRate = gratRate;
}
}