标签: switch-statement

使用switch语句制作一个简单的计算器

我创建了一个程序,使用'switch'语句来制作一个简单的计算器.如果我先取整数输出然后取操作符输出,则b的值始终显示为"0".(代码在这里给出)但是,如果我首先采用运算符输出,程序工作正常.这可能是什么原因?谢谢.

int a;
int b;
char sign;

printf("Enter two required integers: ");
scanf("%d", &a);
scanf("%d", &b);

printf("Enter the operator(+ or - or * or /): ");
scanf(" %s", &sign);


switch(sign){

    case '+': printf("The summation of %d and %d is %d", a,b, a+b);
              break;

    case '-': printf("The subtraction of %d and %d is %d", a,b, a-b);
              break;

    case '*': printf("The product of %d and %d is %d", a,b, a*b);
              break;

    case '/': printf("The division of %d and %d is %d", a,b, …
Run Code Online (Sandbox Code Playgroud)

c calculator switch-statement

-2
推荐指数
1
解决办法
1452
查看次数

一键多用 - Swift

我正在制作一个动作按钮,每次按下按钮时都会实现不同的功能。当我单击一次时,地图变为Satellite,当我再次单击它时,它变为标准,当我再次单击它时,它变为混合,当我再次单击它时,它又回到Satellite

这是我的代码。请让我知道我哪里做错了。

    @IBAction func changeLayout(_ sender: AnyObject) {
    switch (sender.anyObject()) {
    case 0:
        mapView.mapType = MKMapType.satellite
    case 1:
        mapView.mapType = MKMapType.standard
    default:
        mapView.mapType = MKMapType.hybrid
    }

}
Run Code Online (Sandbox Code Playgroud)

我得到的错误说:

“Int”类型的表达式模式不能匹配“Any?”类型的值 视图控制器.swift

xcode switch-statement swift

-2
推荐指数
1
解决办法
3037
查看次数

预期的";" 在c中的函数开关中的"{"标记错误之前

我正在学习C语言作为我的第一语言.我正在使用gcc代码块编译器.我switch-case在观看视频后试图使用这些语句.我收到一个错误,该错误;是在{语句令牌之前所期望的.我试过但找不到任何错误.有人可以帮我解决这个问题吗?

#include<stdio.h>
#include<conio.h>
int main()
{
    int b,sum,avg,multi,choise,s[5],a[3];
    printf("Please select any option\n");
    printf("1. I want addition of numbers.\n");
    printf("2. I want to take average of numbers.\n");
    printf(" 3. I want the multiplication table of a number.\n");
    printf("4. Exit\n");

    Switch(choise)
    {
        case 1:
            printf("You have selected the 1st option,\n");
            printf("Please enter any 5 numbers you want to add.\n");
            gets(s);
            sum=s[0]+s[1]+s[2]+s[3]+s[4];
            printf("The sum of the numbers you entered is %d",sum);
            break;
        case 2:
            printf("You have selected …
Run Code Online (Sandbox Code Playgroud)

c compiler-errors switch-statement

-2
推荐指数
1
解决办法
799
查看次数

嵌套ifs在Java中使用Boolean的替代方法

我有下面的代码块

if(Objects.nonNull(isMine)) {
    if (isMine) {
             this.books= // gets it from the database;
    } else  {
            this. books= // gets it from the database
    }

 } else {
     this. books = // gets it from the database
 }
Run Code Online (Sandbox Code Playgroud)

isMine - 是一个布尔对象,我尝试使用switch case,将isMine转换为字符串,如下所示

 String.valueOf(isMine) 
Run Code Online (Sandbox Code Playgroud)

但是没有用.建议在java中实现上述代码的更好,更快的方法.

java collections if-statement switch-statement java-8

-2
推荐指数
1
解决办法
157
查看次数

错误在我的 C++ 程序中重复大小写值。虽然我什么都改变不了

我试图编写一个程序来帮助我找到给定角度的三角函数值。下面是程序,

    #include<iostream>
#include<cmath>
using namespace std;

void convert(char type);

int main()
{
    char ch;


    cout<<"Enter what angle value u want to calculate: 'c' for cos, 's' for sine and 't' for tan: ",cin>>ch;

    convert(ch);

    return 0;
}

void convert(char type)
{
    float angle;
    double num;
    switch (type)
    {
        case 'C' || 'c':
            cout<<"Enter angle value to calculate cos equivalent: ",cin>>angle;
            num = cos(angle);
            break;
        case 's' || 'S':
            cout<<"Enter angle value to calculate sin equivalent: ",cin>>angle;
            num = sin(angle);
            break; …
Run Code Online (Sandbox Code Playgroud)

c++ case duplicates switch-statement

-2
推荐指数
1
解决办法
1837
查看次数

C中案例范围的语法?

我想在我的代码中创建一个switch语句,其代码遍及大范围的整数(1-15000),分为3个部分:1-5000,5001-10000,10001-15000.

我试过这样一次:

switch(number) {
    case 1 ... 1000: (statement here); 
Run Code Online (Sandbox Code Playgroud)

这给了我一个关于"......"的语法错误 - "预期的a:".

然后我尝试了:

switch (number) {
case 1:
case 5000:
    (statement);
    break;
Run Code Online (Sandbox Code Playgroud)

这个方法确实编译但是没有按预期工作,对于没有明确列出的案例的错误答案.

在网上找不到任何其他建议.有人可以帮忙吗?

c case switch-statement

-2
推荐指数
1
解决办法
330
查看次数

为什么此代码打印0223?

为什么案例2在此for循环中被评估两次?

for (int x = 0; x <4; x++)
{
    switch (x)
    {
        case 2: printf("%d", x);
        case 0: printf("%d", x);
        default: break;
        case 3: printf("%d", x);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我忘了补充一点,这不是一段合法的代码.上周我的朋友工作考试中出现了一些问题.

c c++ for-loop switch-statement

-2
推荐指数
1
解决办法
71
查看次数

默认情况下总是在菜单切换中运行

为什么默认情况总是运行?

public class HelloWorld {
    public enum ScenarioState {
        INIT,
        START,
        STOP,
        DESTROY
    }

    public static void displayRecord(ScenarioState state) {
        switch (state) {
            case INIT:
            case START:
            case STOP:
                System.out.println("1");
            default:
                System.out.println("default");
        }
    }

    public static void main(String[] args) {
        ScenarioState state = ScenarioState.INIT;
        displayRecord(state);
    }
}
Run Code Online (Sandbox Code Playgroud)

预期输出应该是

1

但实际输出:

1
默认

为什么会造成这个问题呢?任何人都可以帮助我

java switch-statement

-2
推荐指数
1
解决办法
2371
查看次数

JAVA上的计算器

package calculator;

import java.util.Scanner;

public class NTimesRunningTwoInputCalculator {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("enter a number = ");
        int a = sc.nextInt();
        System.out.println("enter number of opration = ");
        int n = sc.nextInt();
        int result = 0;
        int remainder = 0;
        for (int i = 1;i<=n;i++) {
            System.out.println("enter another number =");
            int b = sc.nextInt();
            System.out.println("enter operation =");
            sc.nextLine();
            char c = sc.nextLine().charAt(0);
             switch(c) {
             case'+' :
                 result = a+b; …
Run Code Online (Sandbox Code Playgroud)

java for-loop character calculator switch-statement

-2
推荐指数
1
解决办法
51
查看次数

切换案例在android中的onActivityResult中给出奇怪的响应

我有活动,我正在打开相机,语音和视频的意图.我正在使用onActivityResult.但是当onActivityResult被调用时,我正在观察一些非常奇怪的事情,在所有情况下,所有情况下被调用意味着它的gng.

怎么可能.我错了什么?.

以下是我的代码

   protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     {
      super.onActivityResult(requestCode, resultCode, data); 

       if(resultCode == RESULT_OK){




          switch(requestCode) { 

             case 1:

                Uri selectedImage = data.getData();
                System.out.println("selectedimage"+selectedImage);
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage,       filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();


                yourSelectedImage = BitmapFactory.decodeFile(filePath);
                /* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in
                 *  way you want! */



                btn_img.setImageBitmap(yourSelectedImage);





        case 0:
            try {
                yourSelectedImage …
Run Code Online (Sandbox Code Playgroud)

java android break switch-statement android-activity

-3
推荐指数
1
解决办法
1138
查看次数