Три цикла в одной программе, дать возможность в меню выбрать тот или иной - C#
Формулировка задачи:
Ребят подскажите пожалуйста. Имеется вот такое задание:
Дана функция x=a*sin(k*t+2)*cos(k*t). Составить алгоритм для расчета этой функции, если а изменяется от 5 до 7 с шагом 0.12, t изменяется от 4.2 до 6.2 с шагом 0.17, а k=1,2,3,...,12.
Составить три варианта программы с циклами for; while и do-while. Я делаю вот так:
Цикл For:
Цикл While
Цикл do-while:
Но это получились три разные программы. А можно ли это дело сделать в одну программу с меню, в котором бы предлагалось пользователю самому выбирать, с помощью какого цикла считать?
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { double a, t, x; int k; for (a = 5; a <= 7; a += 0.12) for (t = 4.2; t <= 6.2; t += 0.17) for (k = 1; k <= 12; k += 1) { x = a * Math.Sin(k * t + 2) * Math.Cos(k * t); Console.WriteLine("x= " + x); Console.ReadLine(); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication5 { class Program { static void Main(string[] args) { double a, t, x; int k; a = 5; k = 1; t = 4.2; while ((a <= 7) && (t <= 6.2) && (k <= 12)) { x = a * Math.Sin(k * t + 2) * Math.Cos(k * t); Console.WriteLine("x= " + x); a += 0.12; t += 0.17; k += 1; Console.ReadLine(); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { double a, t, x; int k; a = 5; k = 1; t = 4.2; do { x = a * Math.Sin(k * t + 2) * Math.Cos(k * t); Console.WriteLine("x= " + x); Console.ReadLine(); a += 0.12; t += 0.17; k += 1; } while ((a <= 7) && (t <= 6.2) && (k <= 12)); } } }
Решение задачи: «Три цикла в одной программе, дать возможность в меню выбрать тот или иной»
textual
Листинг программы
using System; namespace ConsoleApplication1 { internal static class Program { private static void Main() { // запрашиваем выбор оператора Console.WriteLine("1. Считать с использованием оператора For"); Console.WriteLine("2. Считать с использованием оператора While"); Console.WriteLine("3. Считать с использованием оператора Do-While"); Console.WriteLine("4. Вернуться к коду программы"); string input = Console.ReadLine(); switch (input) { // ссылаемся на новые методы case "1": RunWithFor(); break; case "2": RunWithWhile(); break; case "3": RunWithDo(); break; case "4": return; // при вводе отличной от 1,2,3,4 цифры, выводим следующие: default: Console.WriteLine("Некорректный ввод"); break; } Console.ReadKey(true);} //создаем методы, на которые ссылались, поместив в их тело операторы цикла //метод с оператором for static void RunWithFor() {double a, t, x; int k; for (a = 5; a <= 7; a += 0.12) for (t = 4.2; t <= 6.2; t += 0.17) for (k = 1; k <= 12; k += 1) { x = a * Math.Sin(k * t + 2) * Math.Cos(k * t); Console.WriteLine("x= " + x); Console.ReadLine();}} //метод с оператором while static void RunWithWhile() { double a, t, x; int k; a = 5; k = 1; t = 4.2; while (a <= 7) while (t <= 6.2) while (k <= 12) {x = a * Math.Sin(k * t + 2) * Math.Cos(k * t); Console.WriteLine("x= " + x); a += 0.12; t += 0.17; k += 1; Console.ReadLine();}} //метод с оператором do-while static void RunWithDo() { double a, t, x; int k; a = 5; k = 1; t = 4.2; do { x = a * Math.Sin(k * t + 2) * Math.Cos(k * t); Console.WriteLine("x= " + x); Console.ReadLine(); a += 0.12; t += 0.17; k += 1;} while (a <= 7); while (t <= 6.2) ; while (k <= 12) ;}}}
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д