Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi professor i have a little problem about my code. in my code, i \'m trying to

ID: 3667293 • Letter: H

Question

Hi professor

i have a little problem about my code.

in my code, i 'm trying to change the data of array[] in main at case 6.

when i use method change(array,month) which i create in this class in mehtod main to change the data, it works, but when i print my data again, i find my data does not change. it is still as same as what i input at first.

could you help me to solve this one?

Thank you very much!

import java.util.Scanner;


public class Rainfall
{

   public static void main(String[] args)
   {
       int totalValue=0;
       double averageMonthly=0;
       int again=0;
      
      
       Scanner input= new Scanner (System.in);
      
       int[] array= new int[12];
       String[] month={"January",
               "February",
               "March",
               "April",
               "May",
               "June",
               "July",
               "August",
               "September",
               "October",
               "November",
               "December"};
      
       for (int i=0; i<12;i++)
       {
           System.out.println("Please enter the rainfall values of "+month[i]+" : ");
           array[i]=input.nextInt();
       }
       do{
       System.out.println("Please choose between 6 actions: "
               + " 1. Show the rainfall for each month."
               + " 2. Find the total rainfall for the year."
               + " 3. Find the average monthly rainfall."
               + " 4. Find which month has the most rain."
               + " 5. Find which month has the least rain."
               + " 6. Update the amount of rain about any month from you.");
      
       int option=input.nextInt();
      
           // TODO Auto-generated method stub
       switch (option)
       {
           case 1:
               Rainfall.print(array, month);
               break;
           case 2:
               int caseTwo=total(totalValue,array);
               System.out.println("Total rain value of all months is : "+ caseTwo);
               break;
           case 3:
               double caseThree=average(array,averageMonthly);
               System.out.println("Average rain value of all months is : "+caseThree);
               break;
           case 4:
               String caseFour=mostValue(array,month);
               System.out.println("The month which has the most value is : "+caseFour);
               break;
           case 5:
               String caseFive=leastValue(array,month);
               System.out.println("The month which has the least value is : "+caseFive);
               break;
           case 6:
              
               Rainfall.change(array,month);
              
               break;
           default:
               System.out.println("That's a invalid option, please enter again.");
               break;
       }
       System.out.println("Do you want to do this program again? "
               + " If yes, please enter 1."
               + " If not,please enter any other integer.");
       again=input.nextInt();
       }while(again==1);
      
   }
  
   public static void print(int[] array, String[] month)
   {
       for (int i=0; i<12;i++)
       {
           System.out.println("In "+month[i]+", the value of rainfall is : "+ array[i]);
       }
   }
  
   public static int total(int totalValue,int[] array)
   {
       for (int i=0;i<12;i++)
       {
           totalValue+=array[i];
       }
       return totalValue;
   }
  
   public static double average(int[] array,double averageMonthly)
   {
       int total=0;
       for (int i=0;i<12;i++)
       {
           total+=array[i];
       }
       averageMonthly=total/12;
       return averageMonthly;
   }
  
   public static String mostValue(int[] array,String[] month)
   {
       int temp=0;
       String name="no result";
       for (int i=0;i<12;i++)
       {
           if (temp<array[i])
           {
               temp=array[i];
               name=month[i];
           }
       }
       return name;
   }
  
   public static String leastValue(int[] array,String[] month)
   {
       int temp1=0;
       String name="no result";
       for (int i=0;i<12;i++)
       {
           if (temp1<array[i])
           {
               temp1=array[i];
               name=month[i];
           }
       }
       return name;
   }
  
   public static void change(int[] array, String[] month)
   {
       int months;
       int newValue;
       Scanner reset=new Scanner(System.in);
      
       System.out.println("Please enter the number of month which you want to reset(1-12) : ");
      
       months=reset.nextInt();
  
       System.out.println("Please enter a new value for "+ month[months-1]);
      
       newValue=reset.nextInt();
      
   }
      
}

Explanation / Answer

import java.util.Scanner;

public class Rainfall
{
public static void main(String[] args)
{
int totalValue=0;
double averageMonthly=0;
int again=0;
  
  
Scanner input= new Scanner (System.in);
  
int[] array= new int[12];
String[] month={"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
  
for (int i=0; i<12;i++)
{
System.out.println("Please enter the rainfall values of "+month[i]+" : ");
array[i]=input.nextInt();
}
do{
System.out.println("Please choose between 6 actions: "
+ " 1. Show the rainfall for each month."
+ " 2. Find the total rainfall for the year."
+ " 3. Find the average monthly rainfall."
+ " 4. Find which month has the most rain."
+ " 5. Find which month has the least rain."
+ " 6. Update the amount of rain about any month from you.");
  
int option=input.nextInt();
  
// TODO Auto-generated method stub
switch (option)
{
case 1:
Rainfall.print(array, month);
break;
case 2:
int caseTwo=total(totalValue,array);
System.out.println("Total rain value of all months is : "+ caseTwo);
break;
case 3:
double caseThree=average(array,averageMonthly);
System.out.println("Average rain value of all months is : "+caseThree);
break;
case 4:
String caseFour=mostValue(array,month);
System.out.println("The month which has the most value is : "+caseFour);
break;
case 5:
String caseFive=leastValue(array,month);
System.out.println("The month which has the least value is : "+caseFive);
break;
case 6:
  
Rainfall.change(array,month);
  
break;
default:
System.out.println("That's a invalid option, please enter again.");
break;
}
System.out.println("Do you want to do this program again? "
+ " If yes, please enter 1."
+ " If not,please enter any other integer.");
again=input.nextInt();
}while(again==1);
  
}
  
public static void print(int[] array, String[] month)
{
for (int i=0; i<12;i++)
{
System.out.println("In "+month[i]+", the value of rainfall is : "+ array[i]);
}
}
  
public static int total(int totalValue,int[] array)
{
for (int i=0;i<12;i++)
{
totalValue+=array[i];
}
return totalValue;
}
  
public static double average(int[] array,double averageMonthly)
{
int total=0;
for (int i=0;i<12;i++)
{
total+=array[i];
}
averageMonthly=total/12;
return averageMonthly;
}
  
public static String mostValue(int[] array,String[] month)
{
int temp=0;
String name="no result";
for (int i=0;i<12;i++)
{
if (temp<array[i])
{
temp=array[i];
name=month[i];
}
}
return name;
}
  
public static String leastValue(int[] array,String[] month)
{
int temp1=0;
String name="no result";
for (int i=0;i<12;i++)
{
if (temp1<array[i])
{
temp1=array[i];
name=month[i];
}
}
return name;
}
  
public static void change(int[] array, String[] month)
{
int months;
int newValue;
Scanner reset=new Scanner(System.in);
  
System.out.println("Please enter the number of month which you want to reset(1-12) : ");
  
months=reset.nextInt();
  
System.out.println("Please enter a new value for "+ month[months-1]);
  
newValue=reset.nextInt();
   array[months-1]=newValue; // this needs to be done because you are accepting the value of change not updating it in array
  
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote