1

Have to find the sum of 3 integers, the numbers are 10,15, and 20.

Tried using scanner, but it would not work for some reason. If I tried to close it, it would error.

import java.util.Scanner; //used for question 2
public class firstassignment {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //question 1: the Largest Number
        int num1 = 10;
        int num2 = 5; 
        int num3 = 20;

          if( num1 >= num2 & num1 >= num3)
              System.out.println(num1+" is the largest Number");
          //if num1 is greater or equal to both num2 & 3, then num1 is the largest number.
          // & compares both, does not go left to right like && will.

          else if (num2 >= num1 & num2 >= num3)
              System.out.println(num2+" is the largest Number");
          // otherwise, if num2 is greater of less than num1 and num3, then num2 is the greatest number.

          else
              System.out.println(num3+" is the largest Number");

          //if all else is false, then num3 is the greatest number.
          //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
    {
      //question 2: sum of 3 numbers
        int num1=10;
        int num2=15;
        int num3=20;
        int finalResult=num1+num2+num3;
         System.out.println(finalResult+"is the sum of the three integers");

Although it shows there is no error in the code, it will not output the sum at all.

6
  • share your full class code Commented Sep 15, 2019 at 0:25
  • What is the output? Commented Sep 15, 2019 at 0:26
  • 1
    There is no output for the sum because you already ended the main method with the } above the "question 2" comment, which makes the { right after it start an instance initializer block, which would run whenever an instance of your firstassignment class is created, and that never happens, so the code never runs. Commented Sep 15, 2019 at 0:36
  • Seconding @Andreas until we get more context, because it looks we didn't get the full code paste Commented Sep 15, 2019 at 0:48
  • 1
    Note: && is a "logical AND" Commented Sep 15, 2019 at 0:48

1 Answer 1

1

if you have to use Scanner to get the sum of the three numbers your code should look like this

//question 2: the sum of 3 numbers

import java.util.Scanner;

public class SecondAssignment {

    public static void main(String[] args) {

        Scanner in  = new Scanner(System.in);
        System.out.println("Enter number 1");
        int num1 = in.nextInt();

        System.out.println("Enter number 2");
        int num2 = in.nextInt();

        System.out.println("Enter number 2");
        int num3 = in.nextInt();

        int finalResult=num1+num2+num3;


        System.out.println(finalResult+" is the sum of the three integers");
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.