Python是否实现switch/case或者python开发人员是否想要使用一系列if, elif, else语句?
我正在尝试创建一个非常简单的游戏,其中不同类型的几个玩家试图猜测范围内的数字.我做了一个函数来初始化一个玩家阵列,我用switch-case来做.
出于某种原因,如果我输入一个数字,它会覆盖它之后的所有情况,即使数组中没有足够的单元格.例如,当输入2时,类型为HUMAN的值,它也会创建3和4的情况,并创建类型为COMPUTER和GUMBLER的玩家.
这是代码:
String name;
int count = 0;
System.out.println("How many players will participate?");
players = new Player[reader.nextByte()];
for (Player player: players)
{
count++;
System.out.print("\n1)Name of the player: ");
name = reader.next();
System.out.println("What will be its type? WRITE A NUMBER\n1- Monley\n2-Human\n3-Gumbler\n4-Computer");
switch (reader.nextInt())
{
case 1:
player = new Monkey (name, MAXIMUM, MINIMUM);
System.out.println("A moneky was created");
case 2:
player = new Human (name);
System.out.println("A human was created");
case 3:
player = new Gumbler (name, MAXIMUM, MINIMUM);
System.out.println("A gummbler was …Run Code Online (Sandbox Code Playgroud) 我正在开发菜单驱动程序,以输出家庭作业的邻接矩阵.在我调用其他4个案例中的任何一个后,当我将一个字符作为我的输入时,我的循环无限地运行,我无法弄清楚为什么.
如果我将案例5激活,则会发生这种情况.
但是如果我输入一个字符作为第一个输入,那么它会按预期正确关闭.
我逐步完成了调试器,似乎如果输入一个字符,它将输入为4并且从不接受另一个输入,因此它会一遍又一遍地打印数组.
谁能解释这个功能有什么问题?我这里只有这个功能,因为整个程序大约有300行,不包括注释.但通过一些测试,我已经将我的错误缩小到这个特定的功能,其他人做了他们想要的.
void menu(char graph[][8])
{
bool run = true;
while (run == true)
{
int menuChoice;
cout << "Welcome to the menu" << endl;
cout << "Pick one of the following" << endl;
cout << "1. add connection" << endl;
cout << "2. delete connection " << endl;
cout << "3. show total number of connections " << endl;
cout << "4. show matrix " << endl;
cout << " 5. to exit" << endl;
cout …Run Code Online (Sandbox Code Playgroud) 我是这个网站的新手,但现在已经开了大约一个学期的编程课.我刚才有一个问题,为什么我的程序正在跳过所有情况,进入默认状态,并立即关闭而不停止或做任何事情.这可能只是一件小事,但我一直坐在这里,看不到它.这是代码.谢谢您的帮助:
//Name Game
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
using namespace std;
int main(void)
{
char name, zzz;
cout << "Hello, and welcome to the start of a fantastic new adventure. \nI'm your guide for the day: Sebastian. \nMay I ask what your name is?\n";
cin >> name;
switch (name)
{
case 'alex':
case 'Alex':
cout << "What an absolutely beautiful name. It sends chills down my spine just thinking about it. \nYou're one lucky girl."; …Run Code Online (Sandbox Code Playgroud) 我的问题涉及如何允许编译器在C和C++中实现switch语句.
如果case变量缺失但是没有default提供程序试图"跳转"到奇数计算的偏移量或者是否为了防止控制流程的潜在问题而陷入困境?
我正在为学校制作游戏这是一个基本的老虎机,它将随机生成数字并将数字转换为单独数组中的字符.这似乎不起作用,因为声明完全被忽略了.它没有给出正确的输出,有些时候它们只会出现空白.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*
George Mason
Slots
Date Started: 4/25/16
Date Finished:
Dr K.
*/
void loadScreen();
void spacer();
void tabSpacer();
void clearScr();
int printMachine(char*, int);
int randomNum(int*, int);
int convertNum(int*, char*, int);
int rand_int();
void game();
int main(){
srand(time(NULL));
int stop;
loadScreen();
clearScr();
game();
scanf("%d", &stop);
}
void game(){
int tokens = 5, randomNums[9], x, y = 9;
char randomChars[9], userInput;
randomNum(randomNums, 9);
convertNum(randomNums, randomChars, 9);
printMachine(randomChars, 9);
}
int printMachine(char* randomChars, int y){
printf("-------------\n");
printf("| %c …Run Code Online (Sandbox Code Playgroud) 只是想知道我是否可以在带有switch语句的java中做这样的事情?
switch(a && b){
case 1:
//
case 2:
//
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,当我评论休息时,当我写一个案例(说"开始")它将打印下面输出的所有案例,意味着所有三行都打印,当我写"停止"它打印两个输出"机械停止"和"没有给出命令",为什么我需要在编译器搜索案例时给出break语句,如果匹配则打印相关输出.
import java.util.Scanner;
public class App{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String text = input.nextLine();
switch(text){
case "start":
System.out.println("Mechine Started");
break;
case "stop":
System.out.println("Mechine stopped");
break;
default:
System.out.println("No command given");
}
}
}
Run Code Online (Sandbox Code Playgroud) var index = 30
switch index {
case 10 :
println( "Value of index is 10")
case 20 :
case 30 :
println( "Value of index is either 20 or 30")
case 40 :
println( "Value of index is 40")
default :
println( "default case")
}
Run Code Online (Sandbox Code Playgroud) 我可以在默认情况下引用一个案例而不是复制粘贴整个代码吗?
例如:
switch(n)
{
case(1): //code here...
break;
case(2): //code here...
break;
case(3): //code here...
break;
default: case(2);
break;
}
Run Code Online (Sandbox Code Playgroud)