我对Java真的很陌生,在这里找不到我做错的地方时遇到了麻烦,该程序用于询问销售金额,然后无论销售数量如何均取平均值,该部分似乎工作正常。当它询问运输成本时,它接受第一个输入,但是不管您是否回答y或n,它都会结束,并且程序结束,我是否只是错过了一些真正明显的东西? (我也试图看看其他人如何使其更具可读性,对不起,我不知道自己在做什么,对此感到抱歉。)
尝试更改它,但随后出现错误,因为我是新手,所以我不知道该如何解决自己,这是我第一次学习Java,我的学校要求我们使用NetBeans(在我的情况下为10.0)< / p>
import java.util.*;
public class JavaApplication3
{ //these ones are the attributes. private String Sales1,2,3;
public static boolean b_repeat = true;
private double Sales1;
private double Sales2;
private double Sales3;
//this is a constructor of 2 parameters for Cales class.
public JavaApplication3(double Sales1, double Sales2, double Sales3)
{
this.Sales1 = Sales1;
this.Sales2 = Sales2;
this.Sales3 = Sales3;
}
public static void main(String args[])
{
double avg = computeAverageSales();
System.out.println(" Average of Sales :" + avg);
if (avg > 200) {
System.out.println(" Product has produced a significant amount of revenue!");
}
double chr = computeShippingCharges();
System.out.println(" Total shipping charge :" + chr);
}
public static double computeAverageSales()
{
double avgSales, salesIn, total = 0;
int cnt = 0;
Scanner sc = new Scanner(System.in);
do {
System.out.println("Input Sales");
double sales = sc.nextDouble();
total = total + sales;
cnt++;
System.out.println("Would you like to keep adding Sales? (y/n)");
String repeatLoop = sc.next();
if (repeatLoop.toLowerCase().equals("n"))
b_repeat = false;
} while (b_repeat);
System.out.println(); //Space seperator
avgSales = total / cnt;
return (avgSales);
}
public static double computeShippingCharges()
{
double avgShipping, shippingIn, total = 0;
int cnt = 0;
Scanner sc = new Scanner(System.in);
do {
System.out.println("Input Shipping cost");
double shipping = sc.nextDouble();
total = total + shipping;
cnt++;
System.out.println("Would you like to keep adding Shipping costs? (y/n)");
String repeatLoop = sc.next();
if (repeatLoop.toLowerCase().equals("n"))
b_repeat = false;
} while (b_repeat);
System.out.println(); //Space seperator
avgShipping = total;
return (avgShipping);
}
public double getSales1()
{
return this.Sales1;
}
public void setSales1(double sales)
{
this.Sales1 = sales;
}
public double getSales2()
{
return this.Sales2;
}
public void setSales2(double sales)
{
this.Sales2 = sales;
}
public double getSales3()
{
return this.Sales3;
}
public void setSales3(double sales)
{
this.Sales3 = sales;
}
}
它应该接受任何数量的销售或运输输入,但是无论您做出何种反应,运输都将只允许输入一个输入
System.out.println(“您是否要继续增加运输成本?(y / n)”);
一起,那是我的问题
答案 0 :(得分:0)
简单来说,变量b_repeat
是在使用它的方法之外定义的,并且每次都会被重用。因此,第一次调用computeAverageSales
或computeShippingCharges
时,它都可以正常工作,但是下次b_repeat
已由上一次调用设置为false。尝试将boolean b_repeat = true;
移到每个函数中,看看会发生什么。