Question

I am trying to write an if-else statement in Java with 7 different variables, initialized and...

I am trying to write an if-else statement in Java with 7 different variables, initialized and declared as integers. I want to write an if-else statement that says "You win!" if their sum equals 21, but says "Sum does not equal 21, exiting program." otherwise. If the sum does not equal 21, then I would need to close the keyboard as well.

Homework Answers

Answer #1

Below is your code:

import java.util.Scanner;

public class SumOfNumbers {
        public static void main(String[] args) {
                // initializing keyboard
                Scanner keyboard = new Scanner(System.in);
                // declaring 7 different variables
                int var1, var2, var3, var4, var5, var6, var7;

                // getting input from user for 7 variables
                System.out.print("Enter number #1: ");
                var1 = keyboard.nextInt();
                System.out.print("Enter number #2: ");
                var2 = keyboard.nextInt();
                System.out.print("Enter number #3: ");
                var3 = keyboard.nextInt();
                System.out.print("Enter number #4: ");
                var4 = keyboard.nextInt();
                System.out.print("Enter number #5: ");
                var5 = keyboard.nextInt();
                System.out.print("Enter number #6: ");
                var6 = keyboard.nextInt();
                System.out.print("Enter number #7: ");
                var7 = keyboard.nextInt();

                // calculating sum
                int sum = var1 + var2 + var3 + var4 + var5 + var6 + var7;

                // printing result
                if (sum == 21) {
                        System.out.println("You win!");
                } else {
                        System.out.println("Sum does not equal 21, exiting program.");
                }

                // closing keyboard
                keyboard.close();
        }
}

Output

Enter number #1: 21
Enter number #2: 45
Enter number #3: 1
Enter number #4: 6
Enter number #5: 8
Enter number #6: 14
Enter number #7: 54
Sum does not equal 21, exiting program.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
So I am trying to write a factor generator. User provides an integer and program needs...
So I am trying to write a factor generator. User provides an integer and program needs to return all factors. I have found an example the code that does this function, but I can't understand how it does that. Please explain line by line how this works.     System.out.print("Enter an integer: ");        int number = input.nextInt();        int index = 2; //Prompts user for an integer and assigns it to variable number, and integer index is declared...
Hi I am trying to prove this statement but I'm not sure how to go about...
Hi I am trying to prove this statement but I'm not sure how to go about doing so. The statement is: Let q be greater than or equal to 2 be a positive integer. If for all integers a and a,whenever q|ab, q|a, or q|b, then q is prime.
I am currently having an issue with coding Java through MyProgrammingLab. This is the question: Write...
I am currently having an issue with coding Java through MyProgrammingLab. This is the question: Write a single statement that will print the message "first is " followed by the value of first, and then a space, followed by "second = ", followed by the value of second. Print everything on one line and go to a new line after printing. Assume that first has already been declared as a double and that second has been declared as an int....
I am trying to write a ladder logic diagram for two floors elevator. The elevator has...
I am trying to write a ladder logic diagram for two floors elevator. The elevator has open door, close door, optic sensor, open limit switch, close limit switch, up call, down call, elevator up and elevator down inputs. The outputs: opened door, closed door, 1st floor and 2nd floor. When call down is depressed, it has to show where the elevator at that particular time (1st floor or second floor). If it is on the first floor, the door will...
I am trying to write a program in C language but keep running into errors. Any...
I am trying to write a program in C language but keep running into errors. Any help would be awesome. here is my code I have so far. #include <stdio.h> #include <conio.h> #include <string.h> int main(){    int lenght, i = 0, state = 0;    char line[100];    printf("enter the string for checking of comment line: \n");    gets(line);    while(i < strline(line)){        switch(state){            case 0: if (line[i] == '/'){               ...
Hello! I am coding in c# and I am having trouble using boolean variables and if...
Hello! I am coding in c# and I am having trouble using boolean variables and if statements. The project I am working on is based on the users input, and the code should branch into two different options. However, I am struggling to understand how to take the users input to decide which path to take. Another problem I am having is with my if statements. After the user has inputed information and the code decides to take them down...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card...
JAVA Exercise_Pick a Card Write a program that simulates the action of picking of a card from a deck of 52 cards. Your program will display the rank and suit of the card, such as “King of Diamonds” or “Ten of Clubs”. You will need: • To generate a random number between 0 and 51 • To define two String variables: a. The first String should be defined for the card Suit (“Spades”, “Hearts”, “Diamonds”, “Clubs”) b. The second String...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps is a game played with a pair of dice. In the game, the shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the “coming out” roll) is a 7 (“natural”) or 11 (“yo-leven”), the shooter immediately wins the game. If the coming out...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT