Question

In: Computer Science

Hello I have a question about Java. example So when you put “sum 1 2 3”...

Hello I have a question about Java.

example

So when you put “sum 1 2 3” in console, you do not separate those. Need a space between sum 1, 2 in the console.

What is Sum?

sum 1 2 3

6 (answer)

sum 123456

21

sum 100 2000 3000

5100

sum 20 50 40 1

111

public static void main (String[] args) {

System.out.Println(“What is Sum?”);

String a=“”;

a = scnr.nextLine();

String[] b = a.split(“”);

if(b[0].equals(“sum”) {

}

Please do not change the above code. If I put sum in the console, it calculate the sum.

you may need to convert type from String to double.

I don’t know how to make code for the sum. Please help me by above code.

Solutions

Expert Solution

ANSWER: Here I am giving you the code and output if you have any problem then comment or like it please.

CODE:

import java.util.Scanner;

public class ReferenceMystery {

   /**
   * @param args
   */
   static int getSum(int n) // method to find the sum of a digit
{   
int sum = 0;
  
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
  
return sum;
}
  
   public static void main(String[] args) {
       Scanner scnr= new Scanner(System.in);
      
       System.out.println("What is Sum?");

       String a="";

       a = scnr.nextLine();

       String[] b = a.split(" ");
      
       int add=0;
       if(b[0].equals("sum") ){
           if(b.length==2) { // when your array has only 2 elements only
               int a1=Integer.parseInt(b[1]);
               int add0=getSum(a1);
               System.out.println(add0);
              
           }else {
           for(int i=1;i<b.length;i++)
               add=add+Integer.parseInt(b[i]);
               System.out.println(add);
           }
       }
      
       }

}

OUTPUT:


Related Solutions

Hello There! I have a question to answer please, I don't understand it, it is about...
Hello There! I have a question to answer please, I don't understand it, it is about data communication and networking "Wireshark" Capture ICMP packets with “Wireshark” and analyze the results. Capturing Basics and Filters 1_ Create a filter to show only HTTP traffic with destination port 80. 2_ Create a filter to exclude ARP and ICMP traffic from visualization. 3_ Create a filter to show DNS queries to a specific domain. TIP: Use the keyword contains to search a specific...
hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be added, such as:     247 + 129...
hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! the program should be a C++ PLS Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be...
Hello! I have a question about the following problem, not sure how to approach it... This...
Hello! I have a question about the following problem, not sure how to approach it... This is from Mathematical Statistics and Data Analysis, Chapter 11, Problem 11.41 The Hodges-Lehmann shift estimate is defined to be d =median(Xi −Yj), where X1 , X2 , . . . , Xn are independent observations from a distribution F and Y1,Y2,...,Ym are independent observations from a distribution G and are independent of the Xi. Show that if F and G are normal distributions, then...
For example, I have an input text file that reads: "Hello, how are you?" "You look...
For example, I have an input text file that reads: "Hello, how are you?" "You look good today." "Today is a great day." How can I write a function that inputs each line into a node in a linked list? Thank you. (Note: the input text files could have a different amount of lines, and some lines could be blank.) This is the linked list code: #include <iostream> #include <cstdlib> #include <fstream> class Node { public: Node* next; int data;...
Question about Java ArrayList: how to delet congestive number: eg : [1 2 2 3 2...
Question about Java ArrayList: how to delet congestive number: eg : [1 2 2 3 2 2 1] -----> [1 2 3 2 1] how to get the number that only in one list: eg: [2 2 1], [ 4 4 2 2], [8 8 4 4 2] -------->[1,8] [8 8 4 4 2], [4 4 2 2], [2 2 1] --------->[1,8] Thanks in advance
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
I have 1 done, but am unsure about 2 and 3 You have just been hired...
I have 1 done, but am unsure about 2 and 3 You have just been hired as a new management trainee by Earrings Unlimited, a distributor of earrings to various retail outlets located in shopping malls across the country. In the past, the company has done very little in the way of budgeting and at certain times of the year has experienced a shortage of cash. Since you are well trained in budgeting, you have decided to prepare a master...
I have created a method in java in my menu list and I have to put...
I have created a method in java in my menu list and I have to put all the work into the method but I cannot get it to print and am not sure where I am going wrong. this is my code: public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); String fName = ""; String lName = ""; double hoursWorked; double hourlyRate; int optionOne = 1; int optionTwo = 2; int optionThree...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT