java - Java-用户选择后菜单未启动选项循环(无编译错误)
问题描述
我查看了有关该主题的文档和一些示例,但我似乎找到了该程序无法正常工作的原因。
我需要制作一个有 6 个选项的程序,我使用的是 Clavier.class,它是一个学校制作和批准的用于阅读用户条目的课程。
所以我只做了第一个选项,但是在测试程序时,如果输入为 1,它不会进入第一个循环,程序只会在打印出用户条目后停止。
这是到目前为止的代码(它是法语):
public class Facturation {
public static void main (String [] args) {
char Choix ;
int random;
final String MSG_PRESENTATION = "Programme de facturation a la minute pour"
+ "\n" + "la location de vehicules electriques. ";
final String MENU = "----"
+ "\n" + "MENU"
+ "\n" + "----"
+ "\n" + "1. Louer un vehicule"
+ "\n" + "2. Facturer la remise d'un vehicule"
+ "\n" + "3. Annuler une location"
+ "\n" + "4. Afficher le montant des recettes"
+ "\n"+ "5. Reinitialiser le montant des recettes"
+ "\n" + "6. Quitter le programme"
+ "\n" + "\n"+ "Entrez votre choix";
final String MSG1 = "LOCATION" ;
final String NOCAR1 = "Il n'y a plus de véhicules disponibles.";
System.out.println (MSG_PRESENTATION);
System.out.print (MENU + "\n") ;
Choix = Clavier.lireChar();
while (Choix >=1 && Choix <=6) ;
{
if (Choix == 1) {
System.out.println(MSG1);
double randomDouble = Math.random();
randomDouble = randomDouble * 4 + 1;
int randomInt = (int) randomDouble;
System.out.println(randomInt);
}
}
}
}
第一个选项生成一个从 1 到 4 的数字。我知道问题不在于数字生成器,因为我在单独的课程中尝试过它并且它有效。代码编译。我尝试使用 switch/case 并没有解决问题。它只会在开始时显示菜单,一旦用户输入“1”,程序就会输出1并停止。
谢谢。
解决方案
我更改了int,现在遇到编译错误。我添加了数字 2 并使用了开关:
public class Facturation {
public static void main (String [] args) {
int Choixmenu ;
int random;
final String MSG_PRESENTATION = "Programme de facturation a la minute pour"
+ "\n" + "la location de vehicules electriques. ";
final String MENU = "----"
+ "\n" + "MENU"
+ "\n" + "----"
+ "\n" + "1. Louer un vehicule"
+ "\n" + "2. Facturer la remise d'un vehicule"
+ "\n" + "3. Annuler une location"
+ "\n" + "4. Afficher le montant des recettes"
+ "\n"+ "5. Reinitialiser le montant des recettes"
+ "\n" + "6. Quitter le programme"
+ "\n" + "\n"+ "Entrez votre choix";
final String MSG_ERREUR = "Ceci n'est pas une entree valide.";
final String MSG1 = "LOCATION" ;
final String NOCAR1 = "Il n'y a plus de véhicules disponibles.";
final String MSG2 = " Entrer le numero du vehicule retourne" ;
System.out.println (MSG_PRESENTATION);
System.out.print (MENU + "\n") ;
int Choixmenu = Clavier.lireInt();
Switch (Choixmenu) {
case 1:
System.out.println(MSG1);
double randomDouble = Math.random();
randomDouble = randomDouble * 4 + 1;
int randomInt = (int) randomDouble;
System.out.println(randomInt);
break;
case 2:
System.out.println(MSG2);
break;
default:
System.out.println (MSG_ERREUR);
break;
}
}
}
推荐阅读
- python - 找出 lambda 函数从哪个 IP 运行
- wso2 - WSO2 微积分器向故障序列抛出异常路径
- python - 405 不允许发布方法
- visual-studio-code - VS Code git 操作失败并出现错误“Git:致命:'git@github.com-name' 似乎不是 git 存储库
- html - Jquery - 单击时触发子菜单,然后在第二次单击时关闭
- docker - 将 Storage Admin IAM 角色分配给我的服务帐户时出错
- elasticsearch - 更新 Elasticsearch 中现有字段的映射索引参数
- javascript - 遍历对象数组并显示项目 [REACT JS]
- android - CameraX.unbindAll() 方法发生了什么?
- android-studio - 没有用于颤振的 sdk 路径