I have a list box that has populations. I have another listbox that has the diff
ID: 3684568 • Letter: I
Question
I have a list box that has populations. I have another listbox that has the difference in population for each year. I am trying to assign years to the difference listbox and select the max and min with the corresponding year. The following is my code and a picture of my visual studio. C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Wiseman_ITM_225_HW_6
{
public partial class Form1 : Form
{
//Variables
decimal average;
const int SIZE = 41;
int[] Population = new int[SIZE];
int index = 0;
int i = 0;
int sum;
int increase;
int decrease;
int difference;
int baseYear = 1950;
public Form1()
{
InitializeComponent();
}
private void exitButton_Click(object sender, EventArgs e)
{
Close();
}
private void getButton_Click(object sender, EventArgs e)
{
try
{
StreamReader inputFile;
inputFile = File.OpenText("USPopulation.txt");
while (index < Population.Length && !inputFile.EndOfStream)
{
Population[index] = int.Parse(inputFile.ReadLine());
index++;
//population = inputFile.ReadLine();
//populationlistBox.Items.Add(population);
}
inputFile.Close();
foreach (int value in Population)
{
populationlistBox.Items.Add(value);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void averageButton_Click(object sender, EventArgs e)
{
sum = Population.Sum();
average = sum / SIZE;
avgtextBox.Text = average.ToString();
}
private void increaseButton_Click(object sender, EventArgs e)
{
}
private void diffButton_Click(object sender, EventArgs e)
{
for (i = 1; i < Population.Count(); i++)
{
difference = Population[i] - Population[i - 1];
differencelistBox.Items.Add(difference);
}
}
}
}
Explanation / Answer
Hi consider the sample example method for your reference,
import java.util.Scanner;
import java.text.DecimalFormat; import javax.swing.JOptionPane; public class Population { public static void main(String[] args) { Scanner input = new Scanner(System.in); DecimalFormat formatter = new DecimalFormat("###,###,###,###,###.00"); int startingNumberOfOrganism = 0, daysOverMultiplying = 0; double averageDailyPopulationIncrease, dailyPopulation, percentIncrease; //Ask for startingNumberOfOrganisms. //Validation: Don't accept numbers less than 2 organisms System.out.println("What is the starting number of organisms?"); startingNumberOfOrganism = input.nextInt(); while(startingNumberOfOrganism < 2) { System.out.println("Invalid Input, enter number greater than 2"); System.out.println("What is the starting number of organisms?"); startingNumberOfOrganism = input.nextInt(); } //Ask for averageDailyPopulationIncrease //Validation: Don't accept negative numbers System.out.println("What is the average daily population increase?(provide integer number, ie 50 for %50:"); averageDailyPopulationIncrease = input.nextInt(); while(averageDailyPopulationIncrease < 2) { System.out.println("Invalid Input, enter an integer greater than one"); System.out.println("What is the average daily population increase?(provide decimal number, ie 50 for %50:"); averageDailyPopulationIncrease = input.nextDouble(); } //Ask for daysOverMultiplying //Validation: Don't accept less than 1 System.out.println("Population Increase will occur over how many days?"); daysOverMultiplying = input.nextInt(); while(daysOverMultiplying < 1) { System.out.println("Invalid Input, must enter at least a day!!"); System.out.println("Population Increase will occur over how many days??"); daysOverMultiplying = input.nextInt(); } //loop to display the size of the //population for each day. System.out.println("Here are the results:"); System.out.println("Days Number Of Organism" ); System.out.println("------------------------------" ); int day = 0; dailyPopulation = startingNumberOfOrganism; percentIncrease = averageDailyPopulationIncrease/100; for(int x = 1; x <= daysOverMultiplying; x++) { day++; if(day > 1) { dailyPopulation += (percentIncrease * dailyPopulation); System.out.println(day + " " + formatter.format(dailyPopulation)); } else { System.out.println(day + " " + formatter.format(dailyPopulation)); } } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.