即使在每种情况下都有中断,Switch 也会默认执行

问题描述 投票:0回答:1

所以我试图在每种情况下使用break来打破

switch
while
语句,但即使使用
break
语句,我的程序在执行特定案例代码后仍会继续执行默认值,我' v 现在尝试了几个小时,但我找不到解决方法,我该怎么办?

代码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        //Creates scanner for user input
        Scanner input = new Scanner(System.in);

        //Creates variables to define the size of the array and the value to make the operations
        int arraySize, arrayCalc;

        //Gets the size of the array
        System.out.println("Please enter the size of your array:");
        arraySize = input.nextInt();
        input.nextLine();

        //Repeats if array size is zero
        while (arraySize <= 0) {
            System.out.println("The size of your array is too small, please set a bigger value:");
            arraySize = input.nextInt();
            input.nextLine();
        }

        //Gets the values of the array
        int[] array = new int[arraySize];
        System.out.println("Please enter the values for your array:");
        for (int i = 0; i < arraySize; i++) {
            array[i] = input.nextInt();
        }

        input.nextLine();

        System.out.println("Please, put the char compatible with the operation you want to do:");

        //Loop in case operator is not valid
        boolean validOperator = false;
        while(!validOperator) {

            //Sets the operator
            String operation = input.nextLine();

            switch (operation) {

                case "+":

                    //Addition operation
                    System.out.println("Please enter what value you would like to add to your array:");
                    arrayCalc = input.nextInt();
                    while (arrayCalc < 0) {
                        System.out.println("The value you have entered is too small, please use a bigger number:");
                        arrayCalc = input.nextInt();
                    }

                    //Makes the operation
                    for (int i = 0; i < arraySize; i++) {
                        array[i] = array[i] + arrayCalc;
                    }

                    break;

                case "-":

                    //Subtraction operation
                    System.out.println("Please enter what value you would like to subtract to your array:");
                    arrayCalc = input.nextInt();
                    while (arrayCalc < 0) {
                        System.out.println("The value you have entered is too small, please use a bigger number:");
                        arrayCalc = input.nextInt();
                    }

                    //Makes the operation
                    for (int i = 0; i < arraySize; i++) {
                        array[i] = array[i] - arrayCalc;
                    }
                    break;

                case "*":

                    //Multiplication operation
                    System.out.println("Please enter what value you would like to multiply to your array:");
                    arrayCalc = input.nextInt();
                    while (arrayCalc < 0) {
                        System.out.println("The value you have entered is too small, please use a bigger number:");
                        arrayCalc = input.nextInt();
                    }

                    //Makes the operation
                    for (int i = 0; i < arraySize; i++) {
                        array[i] = array[i] * arrayCalc;
                    }
                    break;

                case "/":

                    //Division operation
                    System.out.println("Please enter what value you would like to divide to your array:");
                    arrayCalc = input.nextInt();
                    while (arrayCalc < 0) {
                        System.out.println("The value you have entered is too small, please use a bigger number:");
                        arrayCalc = input.nextInt();
                    }

                    //Makes the operation
                    for (int i = 0; i < arraySize; i++) {
                        array[i] = array[i] / arrayCalc;
                    }
                    break;

                default:

                    //Exception if input is not a proper operator
                    System.out.println("That's not a valid operator! try \"+\" \"-\" \"*\" or \"/\".");

            }
        }

        //Prints the operation
        System.out.print("Those are your numbers after the operation: ");
        for (int i = 0; i < arraySize- 1; i++) {
            System.out.print(array[i] + ", ");
            if (i == arraySize - 2) {
                System.out.println(array[i + 1] + ".");
            }
        }
    }
}
java switch-statement break
1个回答
0
投票

设置 validOperator = true;在 case 块中的每个break语句之前。

© www.soinside.com 2019 - 2024. All rights reserved.