Question

In: Computer Science

Can you assist me in understand and write the steps in this code? Write a method...

Can you assist me in understand and write the steps in this code?

Write a method named howMany that does not take in any arguments. Use the Scanner class to ask the user to enter a number between 1 and 5. Print One of the following based on their entry.

1    2 3 4 5   Anything Other Number   

“Lonely Num” “Company” "Crowd" "Fun" "Party"     "Only 1 to 5 Please"

Solutions

Expert Solution

import java.util.Scanner;

public class Example1 {
   public static void main(String[] args) {
       howMany();
   }

   private static void howMany() {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter number (1-5)");
       int i=sc.nextInt();
       //storing the messages in array so that we can print directly
       String arr[]= {"Lonely Num", "Company", "Crowd" ,"Fun", "Party"};
       //checking if it is between 1-5
       if(i<1 || i>5)
           System.out.println("Only 1 to 5 Please");
       else
           // printing i-1 as indexes starts from 0 to length-1 (0-4)
           System.out.println(arr[i-1]);
   }
}


Related Solutions

Use induction to solve the problem. Can you show me the steps too? I don't understand...
Use induction to solve the problem. Can you show me the steps too? I don't understand how to solve this. 3+4+5+...+(n+2)=1/2n(n+5) 1+5+52+...+5(n-1)=1/4(5n-1)
Can you fix to me this code plz I just want to print this method as...
Can you fix to me this code plz I just want to print this method as the reverse. the problem is not printing reverse. public void printBackward() {               Node curr = head;        Node prev = null;        Node next = null;               System.out.print("\nthe backward of the linkedlist is: ");               while(curr != null) {            next = curr.next;            curr.next = prev;   ...
Hi can you assist me with this. Can you explain each of the similarities and differences...
Hi can you assist me with this. Can you explain each of the similarities and differences in maybe a short paragraph. Thank you so 3) Provide THREE ways in which Governmental entities are similar to For Profit Entities and THREE ways in which they are different (30 pts )
can you assist me in getting the desired output? Homework 5-3 Write a Java program that...
can you assist me in getting the desired output? Homework 5-3 Write a Java program that prompts the user for an int n. You can assume that 1 ≤ n ≤ 9. You program should use two embedded for loops that produce the following output:     1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 . . . This is the code: import java.util.Scanner; public class PatternOne {    public static void main(String[] args) {...
Can someone explain to me whats happening for every method in this code and the breakdown...
Can someone explain to me whats happening for every method in this code and the breakdown of everything. I added the p2.h file for any reference needed and i need the Implementation of the tree ADT in the p2.h file explained and why it was implemented in that file and not p2.cpp. p2.cpp #include #include "p2.h" #include "recursive.h" using namespace std; static int sum_helper(list_t list, int total) { if(list_isEmpty(list)) { return total; } else return sum_helper(list_rest(list), total + list_first(list)); }...
can you please explain to me in simplest way that i can understand the cyert and...
can you please explain to me in simplest way that i can understand the cyert and march behaviour theory. kindly give an example for it. thank you so much.
Can you please assist me with setting this question in excel and what functions to use...
Can you please assist me with setting this question in excel and what functions to use PROBLEM 5 Minneapolis Health System has bonds outstanding that have four years remaining to maturity, a coupon interest rate of 9 percent paid annually, and a $1,000 par value. a. What is the yield to maturity on the issue if the current market price is $829? b. If the current market price is $1,104? c. Would you be willing to buy one of these...
please can you write the calc_plant method in another way, I don't understand way they assign...
please can you write the calc_plant method in another way, I don't understand way they assign these specific numbers to min, unhappy1...4. the question says if the plant is within two miles or less of a city, the unhappiness is infinite (that is, assign a very large number to the unhappiness for that city). 2) Otherwise, the unhappiness is equal to the population of the city divided by the distance of the plant from the city. The average unhappiness equals:...
catch me if you can (2002) write a report about catch me if you can (2002)...
catch me if you can (2002) write a report about catch me if you can (2002) must be 900 words that fully describes the movie including describing the main characters, the fraud situation in the movie, and how the situation plays out. The second part of the paper should discuss the actual persons and/or companies on which the movie focuses: Identify these actual persons / company; identify the type(s) of fraud(s) are alleged in the film; discuss your personal feelings...
Can you please tell me if this code needs to be fixed, if it does can...
Can you please tell me if this code needs to be fixed, if it does can you please post the fixed code below please and thank you /** * Models a Whole Life Policy. * * @author Tina Comston * @version Fall 2019 */ public class WholeLifePolicy { // instance variables    private double faceValue; // your code here - code the remaining instance field // constants /** * surrender rate. */ public static final double SURRENDER_RATE = .65; /**...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT