In: Computer Science
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.
please bold the solution
import java.util.Scanner;
public class StudentScores {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int SCORES_SIZE = 4;
int[] lowerScores = new int[SCORES_SIZE];
int i;
for (i = 0; i < lowerScores.length; ++i) {
lowerScores[i] = scnr.nextInt();
}
/* Your solution goes here */
for (i = 0; i < lowerScores.length; ++i) {
System.out.print(lowerScores[i] + " ");
}
System.out.println();
}
}
Java program :
StudentScores.java
//import package
import java.util.Scanner;
//Java class
public class StudentScores {
//main() method
public static void main(String[] args) {
//Scanner class Object
Scanner scnr = new
Scanner(System.in);
//variable that store array
size
final int SCORES_SIZE = 4;
//integer array with name
lowerScores
int[] lowerScores = new
int[SCORES_SIZE];
int i;//declaring variable i
//using for loop to take input from
user
for (i = 0; i <
lowerScores.length; ++i) {
//reading each
score entered by user and
//storing in the
array
lowerScores[i] =
scnr.nextInt();
}
//using for loop
for (i = 0; i <
lowerScores.length; ++i) {
//checking
element
if(lowerScores[i]<=0)
{
//if element is negative then assign 0
lowerScores[i]=0;//set element as 0
}
else {
//when element is non zero this means positive
then
//subtract -1 from the element
lowerScores[i]=lowerScores[i]-1;
}
}
//using for loop to print the array
elements
for (i = 0; i <
lowerScores.length; ++i) {
//print each
array element
System.out.print(lowerScores[i] + " ");
}
System.out.println();//used for new
line
}
}
=========================================
Output :