59.2k views
1 vote
Given that two int variables, total and amount, have been declared, write a sequence of statements that: initializes total to 0 reads three values into amount, one at a time. After each value is read in to amount, it is added to the value in total (that is, total is incremented by the value in amount).

1 Answer

4 votes

Answer:

Explained in Java :D

Step-by-step explanation:

import java.util.Scanner; // library used to input values on the program

main{

int total = 0; //first value declared with value "0"

int amount = new int [2]; // Amount will be an array, which is like a table that can store the number of values indicated in [brackets]

Scanner readvalue = new Scanner (System.in);

// Declares a variable of type Scanner in order to be used to enter the user's values

System.out.println("Please insert three numbers to be added below");

// instruction that send a message to the user on screen

//the following for is an instruction used to store each of the three numbers needed, as well as adding up the numbers to the variable total. It will be explained below:

for(int i=0;i=2;i++){

//declares "i" as a counter for the instruction for.

int i = 0 // declares the variable and gives the value to 0

i = 2 // sets the final value to count in 2, because that's the limit of the array "amount"

i++ // adds 1 number each time this instruction is run

System.out.println("value number"+amount[i]");

// sends a message to the user's screen to type a value

amount[i] = readvalue.nextInt();

// stores in the position marked by "i'" the value introduced for the user, by using the variable readvalue and the method .nextInt();

total = total + amount[i];

// adds up the number typed and the current value of total

}

System.out.println("the total is= "+total");

// sends a message to the user's screen printing the value"total"

}

User Matt
by
4.7k points