Java 调用方法进行计算

Java 调用方法进行计算,java,Java,所以我做了一个课程,用来计算醉酒所需的啤酒数量。我的类接收用户输入的啤酒名称、酒精含量,然后是用户的重量进行计算 这是我的整个啤酒课 public class Beer { private String name; private double alcoholContent; //Default apple values (Constructors) public Beer() { this.name = ""; this.alcoholContent =

所以我做了一个课程,用来计算醉酒所需的啤酒数量。我的类接收用户输入的啤酒名称、酒精含量,然后是用户的重量进行计算

这是我的整个啤酒课

public class Beer {

private String name;
private double alcoholContent;

//Default apple values (Constructors)
public Beer()
{
    this.name = "";
    this.alcoholContent = 0.0;
}
//Accessors
public String getName()
{
    return this.name;
}
public double getAlcoholContent()
{
    return this.alcoholContent;
}
//Mutators
public void setName (String aName)
{
    this.name = aName;
}
public void setAlcoholContent (double aAlcoholContent)
{
    if (aAlcoholContent < 0 || aAlcoholContent > 1)
    {
        System.out.println("That is an invalid alcohol content");
        return;
    }
    this.alcoholContent = aAlcoholContent;
}
//Methods
public double Intoxicated (double aWeight)
{
    double numberOfDrinks = (0.08 + 0.015) * aWeight / (12 * 7.5 * this.alcoholContent);
    return numberOfDrinks;
}
这就是输出窗口的外观,接收用户输入的重量,然后执行计算,以查看在之前定义两种啤酒被视为醉酒时,基于用户输入的啤酒数量:

public double Intoxicated (double aWeight)
{
    double numberOfDrinks = (0.08 + 0.015) * aWeight / (12 * 7.5 * this.alcoholContent);
    return numberOfDrinks;
}
饮用上述饮料的人的体重是多少

185

需要3.166瓶“firstBeerName”啤酒才能使人陶醉

需要1.979“Second BeerName”啤酒才能使人陶醉


醉人的公式是给我的,我不知道如何正确设置我的类测试主方法文件,该文件调用该类以反映该输出。

您需要编写一个包含主方法的测试类。在main方法中,可以创建多个
对象

通过迭代你的啤酒,你可以得到想要的结果

  • 查看以获取有关如何设置主方法的信息
  • 在该方法中创建一组酒精含量不同的
    啤酒
    -对象
  • 获得重量的最大值,然后
  • 迭代数组,调用<代码>陶醉()
并打印结果
我鼓励您在setter中检查条件时抛出IllegalArgumentException:

public void setAlcoholContent(double aAlcoholContent) {
    if (aAlcoholContent < 0 || aAlcoholContent > 1) {
        throw new IllegalArgumentException("Alcohol content can't be more than 1 or less than 0");
    }
    this.alcoholContent = aAlcoholContent;
}
public-void-setocholcontent(双aalcholcontent){
如果(酒精含量<0 | |酒精含量>1){
抛出新的IllegalArgumentException(“酒精含量不能大于1或小于0”);
}
此。酒精含量=酒精含量;
}
对于你的问题,你可以这样测试:

public static void main(String[] args) {
    List<Beer> beers = new ArrayList<>();
    beers.add(new Beer("firstBeerName", 0.04));
    beers.add(new Beer("secondBeerName", 0.06));
    Scanner reader = new Scanner(System.in);
    System.out.println("What’s the weight of the person consuming said beverages?");
    double weight = reader.nextDouble();
    DecimalFormat decimalFormat = new DecimalFormat("0.000");
    for(Beer beer : beers){
        System.out.println("It would take " + decimalFormat.format(beer.Intoxicated(weight)) + " " + beer.getName() +" beers to become intoxicated.");
    }
}
public static void main(String [ ] args)
{
Beer beer1 = new Beer().
beer1.setName("firstBeerName");
beer1.setAlcoholContent(3.166);

Scanner reader = new Scanner(System.in);  // Reading from System.in
System.out.println("What’s the weight of the person consuming said beverages?");
double weight = reader.nextDouble(); 

double answer = beer1.Intoxicated(weight);
System.out.println("It would take "+answer+" "+beer1.getName()+" beers to become intoxicated.")


// similar for beer2
}
publicstaticvoidmain(字符串[]args){
列表啤酒=新的ArrayList();
啤酒。添加(新啤酒(“firstBeerName”,0.04));
啤酒。添加(新啤酒(“第二啤酒名”,0.06));
扫描仪阅读器=新扫描仪(System.in);
System.out.println(“饮用上述饮料的人的体重是多少?”);
双倍重量=reader.nextDouble();
DecimalFormat DecimalFormat=新的DecimalFormat(“0.000”);
用于(啤酒:啤酒){
System.out.println(“需要”+decimalFormat.format(beer.toused(weight))+“”+beer.getName()+“beers才能醉酒”);
}
}

您还可以使用循环创建新的啤酒,只需向用户询问可以获得结果的啤酒量,然后创建循环。

您可以编写如下主要方法:

public static void main(String[] args) {
    List<Beer> beers = new ArrayList<>();
    beers.add(new Beer("firstBeerName", 0.04));
    beers.add(new Beer("secondBeerName", 0.06));
    Scanner reader = new Scanner(System.in);
    System.out.println("What’s the weight of the person consuming said beverages?");
    double weight = reader.nextDouble();
    DecimalFormat decimalFormat = new DecimalFormat("0.000");
    for(Beer beer : beers){
        System.out.println("It would take " + decimalFormat.format(beer.Intoxicated(weight)) + " " + beer.getName() +" beers to become intoxicated.");
    }
}
public static void main(String [ ] args)
{
Beer beer1 = new Beer().
beer1.setName("firstBeerName");
beer1.setAlcoholContent(3.166);

Scanner reader = new Scanner(System.in);  // Reading from System.in
System.out.println("What’s the weight of the person consuming said beverages?");
double weight = reader.nextDouble(); 

double answer = beer1.Intoxicated(weight);
System.out.println("It would take "+answer+" "+beer1.getName()+" beers to become intoxicated.")


// similar for beer2
}

您需要创建一个
main
方法,该方法执行以下操作:

1) 打印啤酒值提示(名称和酒精百分比)

2) 接收用户输入的啤酒值

3) 打印用户体重提示

4) 接收用户输入的重量

5) 计算并打印结果

对于打印提示,您很可能希望使用
System.out.println(“此处有提示!”)

要获取输入,您很可能需要使用
扫描仪
。您可以在本网站和其他网站上搜索,以及阅读文档,了解如何使用该类获取输入

以下是一个主要方法的示例:

public static void main(String[] args) {
   Beer blueMoon = new Beer("Blue Moon", 5.4);
   Beer hoegaarden = new Beer("Hoegaarden", 4.9);

   System.out.println("Enter your weight: ");
   Scanner input = new Scanner();
   Double weight = input.nextLine();

   double value = beer1.Intoxicated(weight);

   System.out.println("It would take " + value + " of " + blueMoon.getName() + " to become intoxicated.");

}
我建议将您的
tousined
方法重命名为
tousined
,因为方法名称在Java中通常是大小写的


我不会给你确切的代码,因为这看起来像是家庭作业,我已经毕业了,但这应该足以让你开始。我的建议是四处搜索你提出的任何具体问题

不清楚你想要什么。您不知道如何编写
main
方法?还是运行Java程序?还是获取用户输入?还是打印输出?或者实例化一个
Beer
对象?这两者的某种组合?看起来您的主要障碍是获取用户输入,即人的体重,以便计算啤酒的数量。关于这一点,你可以参考链接的问题。我认为啤酒构造器不正确,不是OP有什么,而是我的建议。