1 / 2100%
private static Object LastNumber;
public static void main(String[] args) throws IOException
{
BufferedReader buf=new BufferedReader(new
InputStreamReader(System.in));
String input;
int i=0;
int []numbers= new int[100];
try
{
do
{
numbers[i]=Integer.parseInt(buf.readLine());
}while(numbers[i++]!=0);
}
catch(IOException e)
{
e.printStackTrace();
}
int min = findMin(numbers, 0, i);
System.out.println("The minimum number is " + min);
int sumOdd = computeSumOfOdd(numbers, 0, i);
System.out.println("The sum of odd numbers is " + sumOdd);
int sum = countNagative(numbers, 0, i);
System.out.println("The count of negative numbers is " + sum);
int sumLess = sumLessThanLast(numbers, 0, i,numbers[i-2]);
System.out.println("The sum of numbers that are less than the last
is " + sumLess);
}
public static int findMin(int[] numbers, int startIndex, int endIndex)
{
if(startIndex==endIndex)
return numbers[startIndex];
int a=(findMin( numbers, startIndex+1, endIndex));
if (numbers[startIndex]<a)
return numbers[startIndex];
else return a;
}
public static int computeSumOfOdd(int[] numbers, int startIndex, int
endIndex)
{
int SumOfOdd = 0;
// Assignment #: 9
// Name: zhang yipeng
// StudentID: 1209849482
// Lecture: mwf 9:40
// Arizona State University CSE205
// Description: will be the construction of a program that reads in a sequence
of integers from standard input until 0 is read, and store them in an array
//then, get the minimum number,sum of odd numbers, count of negative
numbers,sum of numbers that are less than the last.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Assignment9 {
if(startIndex==endIndex)
return SumOfOdd=0;
else
{
SumOfOdd=computeSumOfOdd(numbers, startIndex+1,
endIndex);;
if(numbers[startIndex]%2!=0)
return SumOfOdd+=numbers[startIndex];
else
return SumOfOdd;
}
}
public static int countNagative(int[] numbers, int startIndex, int
endIndex)
{
int count = 0;
if(startIndex==endIndex)
return 0;
else
{
if(numbers[startIndex]<0)
count=1;
else
count=0;
return count+countNagative(numbers, startIndex+1, endIndex);
}
}
public static int sumLessThanLast(int[] numbers, int startIndex, int
endIndex, int lastNumber){
int sum = 0;
if(startIndex==endIndex)
return 0;
else
{
sum=sumLessThanLast(numbers, startIndex+1,endIndex,lastNumber);
if(numbers[startIndex]<lastNumber)
sum=sum+numbers[startIndex];
}
return sum;
}
}
Powered by TCPDF (www.tcpdf.org)
Students also viewed