ArtemChidori 0 / 0 / 0 Регистрация: 25.03.2022 Сообщений: 1 |
||||
1 |
||||
25.03.2022, 20:46. Показов 1929. Ответов 1 Метки нет (Все метки)
не понимаю что не так с кодом
ошибки которые у меня появились:
__________________
0 |
4 / 3 / 1 Регистрация: 25.12.2021 Сообщений: 7 |
|
25.03.2022, 21:27 |
2 |
if (pass1 == pass2 & login1 == login2); Не должно быть точки с запятой, а в остальном все работает.
0 |
IT_Exp Эксперт 87844 / 49110 / 22898 Регистрация: 17.06.2006 Сообщений: 92,604 |
25.03.2022, 21:27 |
Помогаю со студенческими работами здесь
Что не так с кодом? #include <iostream> using namespace std; Что не так с кодом? Что не так с кодом #include <string.h> что не так с кодом?!!
Искать еще темы с ответами Или воспользуйтесь поиском по форуму: 2 |
Оператор else не работает, если ввод в консоль не «Камень, ножницы или бумага», сообщение об исключении не отображается. Что является причиной этого.
using System;
namespace Rock__Paper__Scissors_
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Lets play a game of Rock, Paper, Scissors.");
Console.Write("Enter Rock, Paper or Scissors:");
string userChoice = Console.ReadLine();
Random r = new Random();
int computerChoice = r.Next(3);
//0 = Scissors
if (computerChoice == 0)
{
if (userChoice == "Scissors")
{
Console.WriteLine("Computer chose scissors!");
Console.WriteLine("TIE!");
}
else if (userChoice == "Rock")
{
Console.WriteLine("Computer chose Scissors!");
Console.WriteLine("You WIN!");
}
else if (userChoice == "Paper")
{
Console.WriteLine("Computer chose Scissors");
Console.WriteLine("You LOSE!");
}
}
//1 = Rock
else if (computerChoice == 1)
{
if (userChoice == "Scissors")
{
Console.WriteLine("Computer chose Rock!");
Console.WriteLine("You LOSE!");
}
else if (userChoice == "Rock")
{
Console.WriteLine("Computer chose Rock!");
Console.WriteLine("TIE!");
}
else if (userChoice == "Paper")
{
Console.WriteLine("Computer chose Rock");
Console.WriteLine("You WIN!");
}
}
//2 = Paper
else if (computerChoice == 2)
{
if (userChoice == "Scissors")
{
Console.WriteLine("Computer chose Paper!");
Console.WriteLine("You WIN");
}
else if (userChoice == "Rock")
{
Console.WriteLine("Computer chose Paper!");
Console.WriteLine("You LOSE!");
}
else if (userChoice == "Paper")
{
Console.WriteLine("Computer chose Paper");
Console.WriteLine("TIE!");
}
}
//3 = Exception Handling
else
{
Console.WriteLine("You must enter Rock, Paper or Scissors");
}
}
}
}
2 ответа
Прежде чем продолжить, проверьте значение или userChoice
… Я бы предпочел использовать цикл while
using System;
namespace Rock__Paper__Scissors_
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Lets play a game of Rock, Paper, Scissors.");
Console.Write("Enter Rock, Paper or Scissors:");
string userChoice = Console.ReadLine();
//Check it here in a while loop, until the user gets it
//right, the program will not proceed and loop here
while (userChoice != "Scissors" || userChoice != "Rock" || userChoice != "Paper")
{
Console.Write("You must enter Rock, Paper or Scissors");
userChoice = Console.ReadLine();
}
Random r = new Random();
int computerChoice = r.Next(3);
//0 = Scissors
if (computerChoice == 0)
{
if (userChoice == "Scissors")
{
Console.WriteLine("Computer chose scissors!");
Console.WriteLine("TIE!");
}
else if (userChoice == "Rock")
{
Console.WriteLine("Computer chose Scissors!");
Console.WriteLine("You WIN!");
}
else if (userChoice == "Paper")
{
Console.WriteLine("Computer chose Scissors");
Console.WriteLine("You LOSE!");
}
}
//1 = Rock
else if (computerChoice == 1)
{
if (userChoice == "Scissors")
{
Console.WriteLine("Computer chose Rock!");
Console.WriteLine("You LOSE!");
}
else if (userChoice == "Rock")
{
Console.WriteLine("Computer chose Rock!");
Console.WriteLine("TIE!");
}
else if (userChoice == "Paper")
{
Console.WriteLine("Computer chose Rock");
Console.WriteLine("You WIN!");
}
}
//2 = Paper
else if (computerChoice == 2)
{
if (userChoice == "Scissors")
{
Console.WriteLine("Computer chose Paper!");
Console.WriteLine("You WIN");
}
else if (userChoice == "Rock")
{
Console.WriteLine("Computer chose Paper!");
Console.WriteLine("You LOSE!");
}
else if (userChoice == "Paper")
{
Console.WriteLine("Computer chose Paper");
Console.WriteLine("TIE!");
}
}
}
}
}
0
jPhizzle
4 Июл 2019 в 01:50
Добавлено еще несколько операторов if / if else вместо else. Теперь он выплевывает ошибку исключения, которую я хотел.
Цель создания этого заключалась в том, чтобы практиковать / применять методы if, if else и else, поскольку я пытаюсь изучить C # и прорабатываю некоторые учебные пособия в Интернете. Конечно, есть, наверное, лучшие способы сделать эту игру.
-Нужно добавить какие-то петли (когда я научусь их делать). — Компьютер, кажется, генерирует случайное число в предсказуемой последовательности и не кажется таким уж случайным, поэтому мне нужно работать над улучшением этого.
Используя Систему;
namespace Rock__Paper__Scissors_
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Lets play a game of Rock, Paper, Scissors.");
Console.Write("Enter Rock, Paper or Scissors:");
string userChoice = Console.ReadLine();
Random r = new Random();
int computerChoice = r.Next(2);
//0 = Scissors
if (computerChoice == 0)
{
if (userChoice == "Scissors")
{
Console.WriteLine("Computer chose scissors!");
Console.WriteLine("TIE!");
}
else if (userChoice == "Rock")
{
Console.WriteLine("Computer chose Scissors!");
Console.WriteLine("You WIN!");
}
else if (userChoice == "Paper")
{
Console.WriteLine("Computer chose Scissors");
Console.WriteLine("You LOSE!");
}
}
//1 = Rock
else if (computerChoice == 1)
{
if (userChoice == "Scissors")
{
Console.WriteLine("Computer chose Rock!");
Console.WriteLine("You LOSE!");
}
else if (userChoice == "Rock")
{
Console.WriteLine("Computer chose Rock!");
Console.WriteLine("TIE!");
}
else if (userChoice == "Paper")
{
Console.WriteLine("Computer chose Rock");
Console.WriteLine("You WIN!");
}
}
//2 = Paper
else if (computerChoice == 2)
{
if (userChoice == "Scissors")
{
Console.WriteLine("Computer chose Paper!");
Console.WriteLine("You WIN");
}
else if (userChoice == "Rock")
{
Console.WriteLine("Computer chose Paper!");
Console.WriteLine("You LOSE!");
}
else if (userChoice == "Paper")
{
Console.WriteLine("Computer chose Paper");
Console.WriteLine("TIE!");
}
}
//Exception Handling
if (userChoice != "Scissors")
{
Console.WriteLine("Choose Rock, Paper or Scissors");
}
else if (userChoice != "Rock")
{
Console.WriteLine("Choose Rock, Paper or Scissors");
}
else if (userChoice != "Paper")
{
Console.WriteLine("Choose Rock, Paper or Scissors");
}
}
}
}
0
Thomas Dunbar
4 Июл 2019 в 13:09
using System;
public class SpaceEnginnersThrust { static public void Main () {
/*If you want to find the speed your ship is able to go type speed then enter how many large atmospheric thrusters are on your ship. Then enter it's weight in kilograms.
If you want to know how many large atmospheric thrusters you need on your ship first type thrust then type speed in meters per second. Finally add the weight of your ship in kilograms.
If you want to know how heavy your ship is first type in weight then type your speed in meters per second afterwards enter in how many large atmospheric thrusters your ship has. */
string thrusters = Console.ReadLine();
Console.Write("What thrusters do you want to know about? ");
if (thrusters == "small atmospheric")
{
string option = Console.ReadLine();
Console.Write("Do you want to find weight, speed, or thruster power? ");
if (option == "weight")
{
weight();
}
else if (option == "speed")
{
speed();
}
else {
thrust();
}
}
//Main ends here
static void weight()
{
int kn = 96;
double thrusters1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("You have {0} thrusters on your ship.", thrusters1);
double ms1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("nYour ship can go {0} meters per second", ms1);
double kg1 = thrusters1 * kn * 1000 / ms1;
Console.WriteLine("nYour ship weighs {0} kilograms.",kg1);
}
static void speed()
{
int kn = 96;
double thrusters2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("You have {0} thrusters on your ship.", thrusters2);
double kg2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("rYour ship weighs {0} kilograms.", kg2);
double ms2 = (thrusters2 * kn)/(kg2 / 1000);
Console.WriteLine("Your ship can travel at a speed of {0} meters per second", ms2);
}
static void thrust()
{
int kn = 96;
double ms3 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Your ship can travel at a speed of {0} meters per second",ms3 );
double kg3 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("rYour ship weighs {0} kilograms.", kg3);
double thruster3 = ms3 * (kg3/1000)/kn;
Console.WriteLine("nYour ship will need exactly {0} thrusters.", thruster3);
}
else if (thrusters == "large atmospheric")
{
Console.WriteLine("this isnt working");
}
}
}