Question

Create a C# program in visual studios called FortuneCookie whose Main() method contains an array of...

Create a C# program in visual studios called FortuneCookie whose Main() method contains an array of at least 8 strings with fortune-telling phrases. The program should randomly select 2 different phrases and pass them to a method that displays them. A random number generator should be used to select the phrases from random positions in the array and the length of the array should be used to determine one of the random number generator range boundaries (i.e., do not hardcode a number for the range boundary).

The output should look something like this:

Do you want me to read your fortune? y

Your fortune says:

You will travel to faraway lands.

You are wise beyond your years.

This is what I have so far.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FortuneCookie
{
class Program
{
static void Main()
{
string[] myArray = new string[]

{
"There will be some money coming your way soon.", "You will travel to faraway lands.","Happiness is just around the corner.","You are wise beyond you years.",
"The number 3 is very significant for you.", "I see a tall dark, and handsome stranger in your future.","Lots of suprises are waiting for you.","You'll be getting that promotion you wanted."
};

Random r = new Random();

int s1 = r.Next(myArray.Length);

int s2 = r.Next(myArray.Length);

while(s1 == s2);

{

s2 = r.Next(myArray.Length);

}

Display(myArray[s1], myArray[s2]);

}

public static void Display(string str1, string str2)

{

Console.WriteLine("Your fortune says: ");

Console.WriteLine(str1);

Console.WriteLine(str2);
}

}
}
I'm having trouble figuring out how to add the question "Do you want me to read your fortune y/n? "  to the program allowing the user to enter y or n for the anwser. If y compile the program but if n exit the program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is your code: -

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace FortuneCookie {

class Program {

static void Main() {

bool done = false;

Random r = new Random();

while (!done) {

Console.Write("Do you want me to read your fortune y/n? ");

string choice = Console.ReadLine();

if (choice.ToLower() == "y") {



string[] myArray = new string[]

{

"There will be some money coming your way soon.",

"You will travel to faraway lands.",

"Happiness is just around the corner.",

"You are wise beyond you years.",

"The number 3 is very significant for you.",

"I see a tall dark, and handsome stranger in your future.",

"Lots of suprises are waiting for you.",

"You'll be getting that promotion you wanted."

};



int s1 = r.Next(myArray.Length);

int s2 = r.Next(myArray.Length);

while (s1 == s2)

{

s2 = r.Next(myArray.Length);

}

Display(myArray[s1], myArray[s2]);

} else {

done = true;

}

}

}

public static void Display(string str1, string str2)

{

Console.WriteLine("Your fortune says: ");

Console.WriteLine(str1);

Console.WriteLine(str2);

}

}

}

Output

Do you want me to read your fortune y/n?  y
Your fortune says: 
You will travel to faraway lands.
You'll be getting that promotion you wanted.
Do you want me to read your fortune y/n?  y
Your fortune says: 
You'll be getting that promotion you wanted.
You are wise beyond you years.
Do you want me to read your fortune y/n?  y
Your fortune says: 
Happiness is just around the corner.
You will travel to faraway lands.
Do you want me to read your fortune y/n?  n
Add a comment
Know the answer?
Add Answer to:
Create a C# program in visual studios called FortuneCookie whose Main() method contains an array of...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Note that the main function that I have provided does use <string.h> as it constructs test...

    Note that the main function that I have provided does use <string.h> as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the <string.h> library. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not *...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Coding for MindTap C# Programming Exercise 7-1. Create a program named SalesLetter whose Main() method uses...

    Coding for MindTap C# Programming Exercise 7-1. Create a program named SalesLetter whose Main() method uses several WriteLine() calls to display a sales letter to prospective clients for a business of your choice. Display your contact information at least three times in the letter. The contact information should contain at least three lines with data such as land line phone number, cellphone number, email address, and so on. Each time you want to display the contact information, call a method...

  • Write the Code in C program. Create a random password generator that contains a user-specified number...

    Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...

  • Write a java program that create a 2 dimensional array of type double of size 10...

    Write a java program that create a 2 dimensional array of type double of size 10 by 10. Fill the array with random numbers using a random number generator. Print the array contents. Write a java program that creates a file called data.txt that uses the PrintWriter class. Write the numbers 1 to 42 into the file. Close the file. Attach both files. Thank you in advance

  • Microsoft Excel VBA (Visual Basic for Applications) Programming Language Objectives: Create an array and redim it...

    Microsoft Excel VBA (Visual Basic for Applications) Programming Language Objectives: Create an array and redim it to a size equal to an assigned value of a variable, populate the array with a series of random numbers, output the array to a message box and to a worksheet. Instructions: - Review the variables already declared. You won't need others. - See comments in the code that will act as your guide. Add new code directly after each comment. - Assign a...

  • You will create a program with two methods, the main() method of course and another method...

    You will create a program with two methods, the main() method of course and another method called printArray(). The main method will define an array of 5 elements. A loop will be used to prompt the user to enter 5 numeric values which will go into the 5 array elements. The main() method will then call the printArray() method passing the array by reference, and the array length by value. The printArray() method will then print the contents of the...

  • I am trying to create a program called Count.java that contains a single method called compute...

    I am trying to create a program called Count.java that contains a single method called compute with a string parameter called value. The method should count the number of newlines, words (any sequence of characters separated with whitespace), and characters in a given string and return an array of the three integer values. Use a Scanner to count the number of words in a string. The Scanner.next() method returns the next word, ignoring whitespace.

  • Create a C program that: Within main, declares a linear array consisting of 10 double values....

    Create a C program that: Within main, declares a linear array consisting of 10 double values. You can assign values to the array when it is declared or fill them items manually using scanf () - your choice. Also, declare three doubles: min, max, and average. Then from within main, a function is called. The function should take a pointer to the array declared above and then finds the maximum value, the minimum value, and calculates the average of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT