1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
hoa [83]
3 years ago
12

Can anyone help me figure out why my if statements are not executing properly? When the program executes with 7 as the value for

num it should be executing the first line, but instead it goes to the third and displays, " a single digit."
public static String numberInfo(int num){
//TODO student
String numInfo;

if (num == 7) {
numInfo = "lucky sevens!";
}
if (num == 42) {
numInfo = "the answer to life the universe and everything.";
}
if (num < 10) {
numInfo = "a single digit.";
}
else {
numInfo = "a positive number.";
}
return numInfo;
}
Computers and Technology
1 answer:
mariarad [96]3 years ago
5 0

Answer:

For the value of 7 of num, there are actually two if statements that are true and their contained code is executed.

So your line numInfo = "lucky sevens!"; is executed for sure, but then numInfo gets overwritten with by numInfo = "a single digit.";

To fix it, you have to decide how you want the program to behave, since technically, both numInfo's are equally correct.

- if you want to execute at most one if condition, chain them together like if (...) { ... } else if(...) { ... } etc.

- if you want to return multiple numInfo's, turn it into a collection where you add strings

You might be interested in
On mad max ps4, why does the v6 sound beter and healthier than the v8
nadya68 [22]
Um is that a question?

5 0
3 years ago
Is mayonnaise technically an instrument?
stich3 [128]
No it doesn’t count as an instrument
7 0
3 years ago
write an algorithm that reads to values, determines the largest value and prints the largest value with an identifying message ​
REY [17]

Answer:

I'm unsure of what language you are referring to, but the explanation below is in Python.

Explanation:

a = int(input("Input your first number: "))

b = int(input("Input your second number: "))  

c = int(input("Input your third number: "))

maximum = max(a, b, c)

print("The largest value: ", maximum)

6 0
3 years ago
The ____ tag is used to bring an image into a website. <br> (I need the blank for tag)
Montano1993 [528]
The *image* tag is used to bring an image on the website.
4 0
3 years ago
Read 2 more answers
A(n) systems analyst codes system software, fine-tunes operating system performance, and performs other system software-related
NikAS [45]
In general, a systems analyst is a person who uses analysis and design strategies to solve business problems.
They also analyze an organization’s current computer systems and develop information systems solutions to help the organization run more smoothly.

Now knowing a systems analyst job, therefore the answer to this is “FALSE”.

<span> </span>

6 0
4 years ago
Other questions:
  • A field whose data type is ____ can store text that can be used as a hyperlink address
    5·1 answer
  • Devices such as monitors and printers that are connected to a computer are called ________.
    12·1 answer
  • You would like to create a graph that shows how much of your total investments are in stocks, savings, and mutual funds. You sho
    5·2 answers
  • __________ is a collection of formatting options—such as a frame, a rounded shape, and a shadow—that changes a picture’s overall
    5·1 answer
  • The overall visual look of a chart in terms of its graphic effects, colors, and backgrounds is the:
    5·1 answer
  • Why would it be hard to find the ideal CO2 level if the light intensity were very low?
    9·1 answer
  • Speech about society being normal
    12·1 answer
  • Hi who plays among us
    9·1 answer
  • 1. What year was the Entertainment Software Rating Board created?
    15·1 answer
  • What is used for risk response control?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!