Java 从switch语句中向方法返回变量
出于某种原因,我失去了对switch语句中变量的访问 我不允许使用任何全局变量。Java 从switch语句中向方法返回变量,java,arguments,switch-statement,return-value,scope,Java,Arguments,Switch Statement,Return Value,Scope,出于某种原因,我失去了对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();
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();
}
主要方法是尽可能保持清洁。。。只调用一次菜单
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)
案例2->It应该保留案例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)
在案例1中调用的方法
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)
myvariables被清除时。
我应该能够将clef变量从一个案例传输到另一个案例
你能帮我找出我做错了什么吗?
我使用的是返回语句,我只是不明白它们为什么会消失。我不确定您想要实现什么。代码中存在多个编译错误和逻辑错误。我已经试着按照我的理解纠正它们,并在我修改的地方发布代码(因为它太大了,不能作为注释发布)。希望这对你有帮助
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 :");
}
}
公共类项目{
/**
*@param args
*@param扫描器
*/
公共静态void main(字符串[]args){
String clef=“vide”;
扫描仪输入=新扫描仪(系统输入);
showMenu(in,clef);//将clef作为参数传递-新参数
in.close();
}
/**
*添加了一个额外的参数
*@param谱号
*@修改了新的白痴
*/
公共静态无效显示菜单(扫描仪输入,字符串谱号){
System.out.printf(“******************************************************************************%n”
+“*Choisissez une des options suivantes:$n”
+“*1)塞西尔·拉克莱夫·塞雷特*%n”
+“*2)谱号为*%n”
+“*3)Chiffrer un fichier*%n”
+“*4)Déchiffrer un fichier*%n”
+“*5)退出者*%n”
+“********************************************************%n%n”);
//将clef作为参数传递-新参数
选择(in,clef);
}
公共静态int getNumber(扫描仪输入){
int choice=in.nextInt();
in.nextLine();
回报选择;
}
/**
*添加了一个额外的参数
*@param谱号
*@修改了新的白痴
*/
公共静态无效选项(扫描仪输入、字符串谱号){
试一试{
开关(getNumber(输入)){
案例1:
//删除了重复的变量声明-@修改了新变量
谱号=saisirClef(英寸);
重置菜单(谱号,输入);
//以param-@修改新的白痴身份通过了谱号
显示菜单(在,谱号);
打破
案例2:
//字符串应与equals()方法进行比较-@修改了新字符串
如果(谱号等于(“vide”)){
System.out.println(“Erreur:Aucune clef n'aétésaisie.”);
//以param-@修改新的白痴身份通过了谱号
显示菜单(在,谱号);
}否则{
System.out.println(“谱号secrète est:+clef”);
}
打破
案例3:
chiffrerFichier();
打破
案例4:
dechiffrerFichier();
打破
案例5:
退出应用程序();
打破
违约:
System.out.println(“默认”);
打破
}
}捕获(例外e){
System.out.println(“请输入一个数字”);
//选择(in);;
}
}
/**
*注释此方法,因为它不做任何操作
*@返回
*@修改了新的白痴
*/
/*公共静态字符串afficherClef(){
回音谱号;
}*/
公共静态布尔值isKeyLength(字符串a){
布尔标志=真;
如果(a.长度()<4){
系统输出打印(“错误:Le mot est trop petit”);
flag=false;
}
返回标志;
}
公共静态字符串validateKey(字符串a){
//声明了一个局部变量标志
//只有当sKeyLength(a)和isKeyChar(a)为真时,它才为真
//@修改了新的白痴
布尔标志=isKeyLength(a)和&isKeyChar(a);
国际单项体育联合会(旗){
字符串谱号=a;
系统输出打印项次(谱号);
回音谱号;
}否则{
String clef=“showMenu”;
回音谱号;
}
}
公共静态布尔值isKeyChar(字符串a){
布尔标志=真;
对于(int i=0;i=65&(int)a.charAt(i)我将重新编辑它,它不会以清晰的形式出现。你已经多次声明了标志变量,所以为什么不声明一个标志全局变量来访问所有变量呢?看起来有点混乱…不允许在这个练习中使用任何全局变量。你删除了案例2中我实际需要的唯一方法…请查看我的编辑移动了一个在生活中没有意义的方法,至少它是这样写的。除此之外,我做了很多改变,跟随评论
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 :");
}
}