[英]Java- Menu not starting option loop after user choice(No compilation errors)
我查看了有關該主題的文檔和一些示例,但我似乎找到了該程序無法正常工作的原因。
我需要制作一個有 6 個選項的程序,我使用的是 Clavier.class,這是一所學校制造並批准 class 用於閱讀用戶條目。
所以我只做了第一個選項,但是在測試程序時,如果輸入為 1,則第一個循環不會 go,程序只有在打印出用戶條目后才會停止。
這是到目前為止的代碼(它是法語):
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 的數字。我知道問題不在於數字生成器,因為我在單獨的 class 中嘗試過它並且它有效。 代碼編譯。 我嘗試使用 switch/case 並沒有解決問題。 它只會在開始時顯示菜單,一旦用戶輸入“1”,程序 output 1 就會停止。
謝謝。
這里的問題是您將選擇作為 char 並將其與預期的 int 值進行比較。 在使用比較之前,只需將 choix char 轉換為它的數值。
int choixNumeric = Character.getNumericValue(Clavier.lireChar());
我更改了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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.