Java for beginners in NetBeans

huangapple go评论65阅读模式
英文:

Java for beginners in netbeans

问题

我是新手在Java中,我尝试解决这个问题,但我很难做到

我使用了For循环,但是我什么都没解决,只是添加了参数
我只计算了26周的序列

for(int i = 0; i <= 26; i++)
System.out.println(i);

但我卡在那里,无法做更多。
如果做得不好,我很抱歉,因为我是个初学者,

这里我发布完整的练习

计算细菌传播

创建一个名为bacteriacalculator的程序,用于计算细菌的传播并显示在一定时间后可能感染和死亡的人数。这是一个控制台应用程序,并期望以以下程序参数的形式输入:

参数1:开始计算时感染人数,为int
参数2:计算运行的周数,为int
参数3:每周感染增长百分比,使用double
参数4:该疾病的死亡率百分比,使用double

您的程序接受上述输入,检查其正确性,并确定每周感染人数(将死亡人数)。

因此,您的程序应该(例如)像这样启动:
Viruscalculator 100 26 5.5 3.5
然后在屏幕上产生这个输出:
运行时程序应该如下所示
Viruscalculator将以100人的初始人数开始运行26周,感染率增长25.5%,死亡率为3.5%:
提示:

英文:

I'm new in java and I tried to solve this problem, but I struggle to do it

I used For loop however I couldn't solve anything just add the arguments and
I only calculated the 26-week sequence
with

for(int i = 0; i <= 26; i++)
System.out.println(i);

but I get stuck there and cant do more.
my apologies if it is not well done as I am a beginner,

here I post the full exercise

Calculation of a bacteria spread

Create a program bacteriacalculator that calculates the spread of bacteria and shows how many infected and dead people will probably be after a certain time. It is a console application and expects the following entries in the form of program arguments:

Argument 1: The number of infected people when calculations start, an int
Argument 2: The number of weeks, the calculation has to run, an int
Argument 3: The weekly increase of infections in percent, so use double
Argument 4: The mortality rate for this disease in percent, so use double

Your program takes the input mentioned above, checks for the correctness and determines the number of people being infected (will die) week per week.

So, your program should (as an example) be started like:
Viruscalculator 100 26 5,5 3,5
And then produce this output on screen:
the program should be like this when you run it
Viruscalculator will calculate with 100 persons at the start and run for 26 weeks with an increase of infections of 25,5% and a mortality of 3,5%:
Hint:

答案1

得分: 0

经过仔细审查问题和表格,我得出了以下结论:

  • 该程序接受4个参数:最初感染人数(int infected)、计算持续的周数(int weeks)、每周感染人数增加百分比(double inf)和每周死亡人数百分比(double de)。
  • 每周,程序在新行中显示周数(int w)、感染人数(int infected)和截至该周的死亡人数。
  • 每周,感染人数增加inf% 的infected,并四舍五入到最近的整数。
  • 每周的死亡人数等于de% 的infected,并四舍五入到最近的整数。

基于这个分析,你可以编写一个简单的程序来完成指定的任务:

import java.math.*;

public class Infections
{
    public static void main(int infected, int weeks, double inf, double de) {
    /*
        示例值:
        
        infected = 100;
        weeks = 26;
        inf = 25.5;
        de = 3.5;
    */
        
        System.out.println("周\t感染人数\t死亡人数");
        for (int w=1;w<=weeks;w++) {
            System.out.println(w+"\t"+infected+"\t"+(int)Math.round((de*infected)/100));
            infected += (int)Math.round((inf*infected)/100);
        }
    }
}

我建议你仔细阅读问题陈述,并在尝试解决问题之前对应该完成的任务有一个很好的理解。这样,你就能够有正确的解决方法。

英文:

After examining the question and the table properly, I have come to the following conclusions:

  • The programme takes 4 arguments, the number of people initially infected (int infected), the number of weeks the calculation must go on for (int weeks), the percentage of increase in the number of infections per week (double inf), and the percentage of deaths per week (double de).
  • For every week, the programme, in a new line, displays the week number (int w), the number of people infected (int infected), and the number of deaths till that week.
  • Every week, the infections are incremented by inf% of infected rounded off to the nearest whole number.
  • The number of deaths till every week is equal to de% of infected rounded off to the nearest whole number.

Based on this analysis, you can write a simple programme like this which does the job as specified:

import java.math.*;

public class Infections
{
    public static void main(int infected, int weeks, double inf, double de) {
    /*
        Example values:
        
        infected = 100;
        weeks = 26;
        inf = 25.5;
        de = 3.5;
    */
        
        System.out.println(&quot;Week\tInfections\tDeaths&quot;);
        for (int w=1;w&lt;=weeks;w++) {
            System.out.println(w+&quot;\t&quot;+infected+&quot;\t&quot;+(int)Math.round((de*infected)/100));
            infected += (int)Math.round((inf*infected)/100);
        }
    }
}

I recommend you to read the problem statement thoroughly and have a good understanding of what is supposed to be done before attempting to solve it. That way, you will be able to have the right approach to solving it.

huangapple
  • 本文由 发表于 2020年4月5日 12:01:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61037883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定