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]
2 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]2 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
Does anyone else have edge and think its not the awful?
oksano4ka [1.4K]

Answer:

yea

Explanation:

3 0
3 years ago
Read 2 more answers
What is the answer for 2.8.10 word games? This is what I have so far, but I can’t seem to be able to figure out the bananaSplit
Feliz [49]

Answer:

 public String bananaSplit(int insertIdx, String insertText) {

     return word.substring(0, insertIdx) + insertText + word.substring(insertIdx);

 }

Explanation:

Do you have the other parts of the WordGames class?

8 0
2 years ago
You and your friend play a video game where a superbird has to find and eat radiation leaks at nuclear power plants before the p
koban [17]

Answer:

Bird with superpowers

Explanation:

It is the main idea

7 0
3 years ago
Read 2 more answers
Define an organizational unit in your own words.
Musya8 [376]

Explanation:

An organizational unit (OU) is a subdivision within an Active Directory (hierarchical collection of network resources that can contain users, computers, printers, and other Active Directories) into which you can place users, groups, computers, and other organizational units. Each domain can implement its own organizational unit hierarchy, typically used either to differentiate between objects with the same name, or to parcel out authority to create and manage objects.

I hope you find this information useful and interesting! Good luck!

3 0
3 years ago
Help ME! Will Mark BRAINLIEST! Its Engineering!
Anettt [7]
What is it about ? And what you need help on
5 0
3 years ago
Other questions:
  • You have been asked to create an authentication security plan for your company. Which of the following components would you inco
    14·1 answer
  • What is the next series of dragon ball super
    6·2 answers
  • Your friend called and told you that he saw information about the classified XYZ program on the Internet. As a cleared employee
    14·1 answer
  • In which type of land contract does the seller earn interest on the difference between what the seller owes on an existing loan
    14·1 answer
  • Write a program that will print out statistics for eight coin tosses. The user will input either an "h" for heads or a "t" for t
    14·1 answer
  • ppose we have a Rectangle class that includes length and width attributes, of type int, both set by the constructor. Define an e
    9·1 answer
  • What is TCP/IP adress​
    10·1 answer
  • I need help fixing this please
    6·2 answers
  • Define computers software.dicuss the types of computer software​
    5·1 answer
  • A global clothing company is looking to create a more immersive shopping experience for customers.What is a way the company can
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!