首页 > 解决方案 > 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并停止。

谢谢。

标签: javamenuselection

解决方案


我更改了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;

       }

   }
   }



推荐阅读