解决这个问题? [英] Solve this question?

查看:70
本文介绍了解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序要求用户输入五个以空格分隔的单个字符,根据字符,它会生成一个随机数。



以下是规则完全按照:

- 如果输入字符是'a',程序将生成一个从1到20的随机数。

- 如果输入字符是'e',程序将生成一个从21到40的随机数。

- 如果输入字符是'i',程序将生成一个从41到60的随机数。

- 如果输入字符是'o',程序将生成一个从61到80的随机数。

- 如果输入字符是'u',程序将生成一个从81到100的随机数。

- 如果输入字符不是上述字符之一,则在输出中显示0。



以下是一些示例运行:



运行1



这个程序播放一个简单的随机n umber游戏。

输入以空格分隔的5个元音字符(a,e,i,o,u):aeiou

随机数为10 25 46 69 99

-------------------------------------------- -------------------

运行2



这个程序很简单随机数游戏。

输入由空格分隔的5个元音字符(a,e,i,o,u):aeauo

随机数为10 25 4 99 66

------------------------------------------- --------------------

运行3



此程序播放简单随机数游戏。

输入由空格分隔的5个元音字符(a,e,i,o,u):azieo

随机数为10 0 43 29 76

------------------------------------------ ---------------------

运行4



此程序播放一个简单的随机数游戏。

输入由空格分隔的5个元音字符(a,e,i,o,u):azifo

随机数a re 15 0 47 0 74



我尝试过:



The program asks user to enter five single characters separated by spaces, and depending on the characters, it will generate a random number.

Here are the rules to follow exactly:
- If input character is 'a', program will generate a random number from 1 to 20.
- If input character is 'e', program will generate a random number from 21 to 40.
- If input character is 'i', program will generate a random number from 41 to 60.
- If input character is 'o', program will generate a random number from 61 to 80.
- If input character is 'u', program will generate a random number from 81 to 100.
- If input character is not one of the above characters, display a 0 in the output.

Here are some sample runs:

Run 1

This program plays a simple random number game.
Enter 5 vowel characters (a,e,i,o,u) separated by spaces: a e i o u
The random numbers are 10 25 46 69 99
---------------------------------------------------------------
Run 2

This program plays a simple random number game.
Enter 5 vowel characters (a,e,i,o,u) separated by spaces: a e a u o
The random numbers are 10 25 4 99 66
---------------------------------------------------------------
Run 3

This program plays a simple random number game.
Enter 5 vowel characters (a,e,i,o,u) separated by spaces: a z i e o
The random numbers are 10 0 43 29 76
---------------------------------------------------------------
Run 4

This program plays a simple random number game.
Enter 5 vowel characters (a,e,i,o,u) separated by spaces: a z i f o
The random numbers are 15 0 47 0 74

What I have tried:

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>

using namespace std;

/* Homework 3
Yong Yu Xuan
This program generates a random number depending on the
character inputted.
*/

int main()
{
    srand(time(0));

    //constant for setw()
    const int GAP = 6;

    //variables
    int num;

    int none = 0;
    char alphabet;
    char letter1, letter2, letter3, letter4, letter5;
    letter1 = 'a';
    letter2 = 'e';
    letter3 = 'i';
    letter4 = 'o';
    letter5 = 'u';

    //tell user to enter five characters at random
    cout << "This program plays a simple, random number game." << endl;
    cout << "Enter five vowel characters (a,e,i,o,u) in any order separated ";
    cout << "by spaces: ";
    cin >> alphabet >> alphabet >> alphabet >> alphabet >> alphabet;

    alphabet = num;

    cout << "The five random numbers are: " << setw(GAP) << num << setw(GAP);
    cout << setw(GAP) << num << setw(GAP) << num;
    cout << setw(GAP) << num << setw(GAP) << num;

    if (alphabet == letter1)
        cout << setw(GAP) << rand() % 19 + 1;

    else if (alphabet == letter2)
        cout << setw(GAP) << rand() % 19 + 21;

    else if (alphabet == letter3)
        cout << setw(GAP) << rand() % 19 + 41;

    else if (alphabet == letter4)
        cout << setw(GAP) << rand() % 19 + 61;

    else if (alphabet == letter5)
        cout << setw(GAP) << rand() % 19 + 81;

    else
        cout << none << endl;

    return 0;
} 

推荐答案

此行非常可疑,它在1个变量中读取5个值

This line is highly suspect, it read 5 values in 1 variable
cin >> alphabet >> alphabet >> alphabet >> alphabet >> alphabet;



什么是将其余程序应用于每个值的魔力?

但这并不重要,因为这个


what is the magic that will apply the rest of program to each values?
but it doesn't really matter because this one

alphabet = num;



正在杀死该值。

我害怕你必须重新考虑你的程序,整个组织是错误的。

-----

当你不明白你的代码在做什么或为什么它做什么的时候是的,答案是调试器

使用调试器查看代码正在做什么。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期效果时,你就接近一个bug。

-----

掌握一些分析方法可以提供帮助, Dijkstra自上而下的方法是一个良好的开端。

https:// en .wikipedia.org / wiki / Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/wiki/Structured_programming [ ^ ]

< a href =https://en.wikipedia.org/wiki/Edsger_W._Dijkstra> https://en.wikipedia.org/wiki/Edsger_W._Dijkstra [ ^ ]

HTTPS://www.cs.utexas。 edu / users / EWD / ewd03xx / EWD316.PDF [ ^ ]


is killing the value.
I fear you have to rethink your program, whole organization is wrong.
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
-----
Mastering some analyze methods can help, Dijkstra Top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]


这篇关于解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆