【问题标题】:returning a variable to a method from within a switch statement从 switch 语句中将变量返回给方法
【发布时间】:2013-05-18 03:45:25
【问题描述】:

由于某种原因,我无法访问 switch 语句中的变量,

我不能使用任何全局变量。

import java.util.Scanner;
public class Projet {

    public static void main(String[] args) {
        String clef="vide";
        Scanner in = new Scanner(System.in);
        showMenu(in);
        in.close();
        }

Main 方法尽可能保持干净...只调用一次菜单。

public static void showMenu(Scanner in){
    System.out.printf("******************************************%n" +
            "* Choisissez une des options suivantes : *%n" +
            "* 1) Saisir la clef secrète              *%n" +
            "* 2) Afficher la clef secrète            *%n" +
            "*******************************************%n%n%n");
    choice(in);
}

showMenu(in) 根据所做的选择,我们将进入特定案例。

public static int getNumber(Scanner in){
    int choice = in.nextInt();
    in.nextLine();
    return choice;
}

getNumber(in) 返回我们之前的输入,以便我们进入案例。双重功能,将在下一个版本中删除。

public static void choice(Scanner in){

    try {

        switch(getNumber(in)){

案例 1 -> 我们假设保留saisirClef(in)返回的谱号变量

        case 1:
            String clef = saisirClef(in);
            break;

案例 2 -> 它应该保留案例 1 中的值

        case 2:
            afficherClef();
            break;      

            default:
                System.out.println("Default");
                break;
            }
        }   catch (Exception e) {
            System.out.println("Please enter a number");
            //choice(in);
        }
    }

saisirClef(in) 在案例一中调用的方法。

public static String saisirClef(Scanner in){
    System.out.println("Saisir la clef secrète  :");
    String a = in.nextLine();
    System.out.println("Voici ce que vous avez tapper : "+a);
    return a;
}

afficherClef 案例 2 中调用的方法

public static String afficherClef() {
        return clef;
    }

}

每次showMenu(in) 我的变量都会被清除。 我应该能够在不同的情况下传输谱号变量......

你能帮我弄清楚我在这里做错了什么吗? 我正在使用 return 语句,我只是不明白它们为什么会消失。

【问题讨论】:

  • 我要重新编辑,结果不是很清楚
  • 你已经多次声明了标志变量,所以为什么不声明一个标志全局变量来访问所有变量......看起来有点混乱......
  • 不允许在这个练习中使用任何全局变量

标签: java arguments switch-statement return-value scope


【解决方案1】:

我不确定您要达到的目标。您的代码中有多个编译和逻辑错误。我已尝试根据我的理解更正它们,并将代码(因为它太大而无法作为评论发布)与我进行更改的 cmets 一起发布。希望对您有所帮助。

public class Project {

/**
 * @param args
 * @param Scanner
 */
public static void main(String[] args) {
    String clef = "vide";
    Scanner in = new Scanner(System.in);

    showMenu(in,clef);// passed clef as a parameter - The New Idiot
    in.close();

}

/**
 * added an extra parameter
 * @param clef
 * @modified The New Idiot
 */
public static void showMenu(Scanner in,String clef) {
    System.out.printf("******************************************%n"
            + "* Choisissez une des options suivantes : *%n"
            + "* 1) Saisir la clef secrète              *%n"
            + "* 2) Afficher la clef secrète            *%n"
            + "* 3) Chiffrer un fichier                 *%n"
            + "* 4) Déchiffrer un fichier               *%n"
            + "* 5) Quitter                             *%n"
            + "******************************************%n%n%n");
    // passed clef as a parameter - The New Idiot
    choice(in,clef);
}

public static int getNumber(Scanner in) {
    int choice = in.nextInt();
    in.nextLine();
    return choice;
}

/**
 * added an extra parameter
 * @param clef
 * @modified The New Idiot
 */
public static void choice(Scanner in,String clef) {

    try {

        switch (getNumber(in)) {
        case 1:
            // removed duplicate variable declaration - @modified The New Idiot
            clef = saisirClef(in);
            resetMenu(clef, in);
            // passed clef as param - @modified The New Idiot
            showMenu(in,clef);
            break;
        case 2:
            //String should be compared with equals() method - @modified The New Idiot
            if (clef.equals("vide")) {
                System.out.println("Erreur : Aucune clef n’a été saisie.");
                // passed clef as param -@modified The New Idiot
                showMenu(in,clef);
            } else {
                System.out.println("La clef secrète est :" + clef);
            }
            break;
        case 3:
            chiffrerFichier();
            break;
        case 4:
            dechiffrerFichier();
            break;
        case 5:
            quitApplication();
            break;
        default:
            System.out.println("Default");
            break;
        }
    } catch (Exception e) {
        System.out.println("Please enter a number");
        // choice(in);
    }
}

/**
 * Commented this method, as it does nothing
 * @return
 * @modified The New Idiot
 */
/*public static String afficherClef() {
    return clef;
}*/

public static boolean isKeyLength(String a) {
    boolean flag = true;
    if (a.length() < 4) {
        System.out.println("Erreur : Le mot est trop petit");
        flag = false;
    }
    return flag;
}

public static String validateKey(String a) {

    // declared a local variable flag
    // it will be true only if sKeyLength(a) and isKeyChar(a) are true
    // @modified The New Idiot
    boolean flag = isKeyLength(a) && isKeyChar(a);

    if (flag) {
        String clef = a;
        System.out.println(clef);
        return clef;
    } else {
        String clef = "showMenu";
        return clef;
    }
}

public static boolean isKeyChar(String a) {
    boolean flag = true;
    for (int i = 0; i <= a.length() - 1; i++) {
        // Verifie Majuscule
        if ((int) a.charAt(i) >= 65 && (int) a.charAt(i) <= 90) {
            System.out.println("Au moins une des lettre est en Majuscule");

            // Continue Program
        } else {
            System.out
                    .println("Erreur : Au moins un caractère est invalide ");
            flag = false;
        }
        // Verifie Doublons
        if (i == a.length() - 1) {
            if (a.charAt(i) == a.charAt(i - 1)) {
                System.out
                        .println("Erreur : Il existe au moins un doublon.");
                System.out.println("Dernier iteration");
                // Restart Program
                flag = false;
            } else {
                // Continue Program
                System.out.println("No doubles found");
                System.out.println("Dernier iteration");
            }
        } else {
            if (a.charAt(i) == a.charAt(i + 1)) {
                System.out
                        .println("Erreur : Il existe au moins un doublon.");
                System.out.println("Iteration :" + i);
                // Restart Program
                flag = false;
            } else {
                // Continue Program
                System.out.println("No doubles found");
            }
        }
    }
    return flag;
}

public static String saisirClef(Scanner in) {
    System.out.println("Saisir la clef secrète  :");
    String a = in.nextLine();
    System.out.println("Voici ce que vous avez tapper : " + a);
    return validateKey(a);
}

public static void resetMenu(String clef, Scanner in) {
    if (clef == "showMenu") {

        saisirClef(in);
    } else {
        System.out.println("Clef saisie avec succes");
    }
}

public static void chiffrerFichier() {
    System.out.println("Chiffrer un fichier :");
}

public static void dechiffrerFichier() {
    System.out.println("Déchiffrer un fichier :");
}

public static void quitApplication() {
    System.out.println("Quitter :");
}

}

【讨论】:

  • 您删除了我在案例 2 中实际需要的唯一方法 ...请参阅我的编辑
  • 我去掉了生活中没有目的的方法,至少是这样写的。除此之外,我在 cmets 之后做了很多更改
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 2021-09-06
相关资源
最近更新 更多