import java.util.Scanner;
public class IndenteurPseudocode {
public static void main(String[] args) {
System.out.println("Ce programme permet de corriger l'indentation d'un algorithme ecrit en pseudocode.\n");
System.out.println("----");
System.out.println("Menu");
System.out.println("----");
System.out.println("1. Indenter pseudocode");
System.out.println("2. Quitter\n");
Scanner myObj = new Scanner(System.in);
String choixMenu;
boolean valid = false;
while(!valid) {
System.out.print("Entrez votre choix : ");
choixMenu = myObj.nextLine();
if ( choixMenu == "1" || choixMenu == "2") {
valid = true;
System.out.print(choixMenu);
} else {
System.out.println("invalid");
}
}
}
}跑
Ce programme permet de corriger l'indentation d'un algorithme ecrit en pseudocode.
----
Menu
----
1. Indenter pseudocode
2. Quitter
Entrez votre choix : 1
invalid
Entrez votre choix : 2
invalid
Entrez votre choix : 3
invalid
Entrez votre choix : 我使用了while循环,但是对于用户选择的任何输入,结果都是相同的。
发布于 2022-11-16 02:23:57
因为您处理的是字符串,所以必须使用.equals()作为==,将这两个字符串作为单独的对象进行比较,并始终返回false (除非两个字符串引用内存中的同一个对象)。
if ( choixMenu.equals("1") || choixMenu.equals("2")){
//code
}您还可以将输入数字解析为整数,并保留==。
int choixMenu;
//...
choixMenu = myObj.nextInt();
if ( choixMenu == 1 || choixMenu == 2) {
//code
}https://stackoverflow.com/questions/74454134
复制相似问题