Question

In: Computer Science

(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the...

(JAVA)
Implementing a Program
Design a program that prompts the user for twenty numbers. If the number is positive, then add the number to the current sum. If the number is negative, then subtract the sum by one. Implement just the main method. Assume that all libraries are imported, and class has been declared.

Solutions

Expert Solution

Java code:

import java.util.Scanner;
public class Main{
  
//required main function(from line 4 to 25) sample class is written only to print the output
   public static void main(String[] args){
       Scanner input=new Scanner(System.in);
       //initializing number
       int num,sum=0;
       //asking for num
       for(int i=0;i<20;i++){
       //asking for number
       System.out.print("Enter the number: ");
       //accepting it
       num=input.nextInt();
       //checking if the number is positive
       if(num>=0)
       //adding the number to sum
       sum+=num;
       else
       //decrementing sum by 1
       sum--;
       }
       //printing sum
       System.out.println("Sum="+sum);
   }

  
}


Screenshot:


Input and Output:


Related Solutions

1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to...
TO BE DONE IN JAvA Complete the calculator program so that it prompts the user to enter two integers and an operation and then outputs the answer. Prompt the user three times, don't expect all the inputs on one line. Possible operations are + - * / No other words or symbols should be output, just the answer. Requested files import java.util.Scanner; /** * This is a simple calculator Java program. * It can be used to calculate some simple...
Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
Using Java, design a program to let user enter two lists of numbers within the range...
Using Java, design a program to let user enter two lists of numbers within the range [0, 9] from keyboard, you could either use flag to denote end of the list or ask user to enter the list size first and then enter the list numbers. Each of these two lists represents a set (keep in mind that duplicate elements are not allowed in the same set but are allowed between sets), so we have two sets A and B....
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT