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
SOMEONE PLEASE HELP ME OUT WITH THIS!!!!!!
mario62 [17]

Answer:

C.

Explanation:

8 0
3 years ago
Read 2 more answers
What is the result of expression 15 &gt; 10 &gt; 5 in C? What is the result of the same expression in Java?
Inessa05 [86]

Answer:

java: error

C: false

Explanation:

In Java the compiler understand that you are trying to compare an integer (15) with a boolean (10 > 5) this generate the next error:

error: bad operand types for binary operator  

In C the compiler convert (15 > 10 > 5) in (15>10) > (10>5) which is equal to TRUE > TRUE, the compiler can also read it as 1 > 1 (since 1 is TRUE and 0 is FALSE).  like 1>1 is false then C program return false.

5 0
3 years ago
In which situation is coauthoring of presentations primarily utilized?
Tanya [424]

Answer:

A i belive

Explanation:

3 0
3 years ago
Read 2 more answers
When Building a resume what would i put when it says field of study especially when your still going to high school?​
Nastasia [14]
Just put Still attending or Enter Highschool then if they ask you about it just let them know your still attending, that’s what I did and got the job on the spot
6 0
3 years ago
_______a file transfers a copy of the file from the storage media into your computer's memory?
Nana76 [90]
It's opening. I just took PF test (:
5 0
4 years ago
Read 2 more answers
Other questions:
  • One main advantage of CD-ROMs is that
    14·1 answer
  • What formula would be used to add two cells together after the equal sign?
    9·2 answers
  • TOPIC-PYTHON
    8·2 answers
  • Cheri's teacher told her to write a program that prompts the user for information. Which function should she use? input() int()
    14·1 answer
  • Low-end CRM systems are designed for companies like Boeing because they only have a few, large customers.
    11·1 answer
  • For what kind of work would you use a block plane?
    10·1 answer
  • How do you change the number of rows and columns displayed for an embedded Excel object on a PowerPoint slide?
    8·1 answer
  • What is the smallest amount of information called?
    13·2 answers
  • Please help i will give you brainelst answer part B thank you!!!!!!!!!!!!!1
    13·1 answer
  • The people on this platform are unbelievably nice. its almost rare to see this type of kindness online these days. Just wanted t
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!