2014-11-09 1 views
-1

я тару преобразовать это в программу Java:я пытаюсь решить алгоритм с Java и я нашел некоторые проблемы

$ java SpaceTravel 
Welcome to the SpaceTravel agency 
What do you want do? [h for help] 
a 
Unknown command. Type h for help 
What do you want do? [h for help] 
h 
h: print this help screen 
q: quit the program 
What do you want do? [h for help] 
q 
Bye bye! 

Теперь проблема, что моя программа, кажется, сделаем бесконечный цикл на 2 do while петля, что бы ни был моим выбором, я попробовал много алгоритмов и стали работать не для меня. вот мой код:

package gesitionEleve; 

import java.util.Scanner; 


public class SpaceTravel { 

    public static void main(String[] args) { 

     System.out.print("Welcom to the SpaceTravel Agency\n"); 


     int lafin = 0; 

     while (lafin != 1) { 

      int taill; 
      do { 
       System.out.print("What do you want to do [h for help] : "); 
       Scanner sc = new Scanner(System.in); 
       String test = sc.nextLine(); 
       taill = test.length(); 
      } while(taill == 0); 

      char choix = 0; 

      String test; 
      if (choix != 'h') { 

       do { 
        System.out.print("\nUknown command. Type h for help "); 
        System.out.print("\nWhat do you want to do : "); 
        Scanner sc1 = new Scanner(System.in); 
        test = sc1.nextLine(); 
        choix = test.charAt(0); 
       } while(choix == 'h'); 

      } 

      System.out.print("\nh : print this help page "); 
      System.out.print("\nq : quite the program ");      

      do { 
       Scanner sc1 = new Scanner(System.in); 
         test = sc1.nextLine(); 
         choix = test.charAt(0); 

         switch(choix) { 
          case 'h' : 
          System.out.print("\nh : print this help page "); 
          System.out.print("\nq : quite the program "); 
          case 'q' : 
          System.out.print("Bye bye"); 
          lafin++; 
         } 
      } while (choix == 'q' || choix == 'h'); 
     } 

    } 

} 

ответ

1

Ниже программа соответствует вашим потребностям:

import java.util.Scanner; 

public class SpaceTravel 
{ 
     public static void main(String[] args) { 

     System.out.print("Welcome to the SpaceTravel Agency\n"); 

     String str; //To avoid exception when user enters just an enter 

     while (true) { //infinite loop 

      char choix; //for getting a character 

      System.out.print("What do you want to do [h for help] : "); 
      Scanner sc = new Scanner(System.in); 
      str=sc.nextLine(); //get input 
      if(str.length()!=1) //If no characters or more than one character is entered, 
      { 
       System.out.println("Invalid Choice"); 
       continue; 
      } 
      choix=str.charAt(0); //get char from str 

      if(choix=='q') //if char is q,break out of the while loop 
       break; 
      if (choix != 'h') { //if char is not h,print invalid input and continue the loop 

       System.out.println("\nUnknown command. Type h for help "); 
       continue; 

      } 

      System.out.print("\nh : print this help page "); 
      System.out.print("\nq : quit the program ");   

     } 

     } 
} 
0

Вы получили условие этого цикла назад.

Если вы хотите оставить петлю, когда «з» введен, он должен быть:

  do { 
       System.out.print("\nUknown command. Type h for help "); 
       System.out.print("\nWhat do you want to do : "); 
       Scanner sc1 = new Scanner(System.in); 
       test = sc1.nextLine(); 
       choix = test.charAt(0); 
      } while(choix != 'h'); 

В настоящее время вы находитесь в цикле, пока вводится «ч».

Следует также отметить, что test.charAt(0) выдаст исключение, если пользователь нажимает ввод, не набирая никаких символов.

Смежные вопросы