我正在尝试使用Java解决算法,但我发现了一些问题

EE

我正在尝试将其转换为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!

现在我的程序似乎在第二个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');
        }

    } 

}
斯皮卡特里克斯

以下程序适合您的需求:

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 ");           

        }

      } 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用javascript解决算法问题

我正在尝试在 python 中使用替换功能,但遇到了一些问题

我正在尝试通过 Rust 中的 OpenWeather API 获取数据,但我想我在解析方面遇到了一些问题

我正在尝试在R中运行XGBoost,但遇到了一些问题

我是 codeigniter 的新手,我正在尝试加入 3 个表,但它给出了一些问题

我已经写了一些代码,我正在尝试使用“else”,但我不断收到错误

我正在尝试使方法类似于ArrayList类中的lastIndexOf(),但是我在查找元素的最后一个索引时遇到了一些问题

我正在OCaml中进行区分功能,但遇到了一些问题

我正在尝试在Ruby上执行股票选择器方法,但是我的代码中存在一些问题

我的登录表单中有一些问题,如何解决?

我该如何解决hashmap模块中的一些问题?

我在期末考试前尝试一些问题

我使用cudaEvent来衡量代码的运行时间。但是我发现了一些难题

我对 javascript 嵌套承诺有一些问题

我对XML布局有一些问题

我在python中的xpath有一些问题

关于我的网站的一些问题

我的JavaScript遇到了一些问题

我的DrawerLayout有一些问题

我刚刚发现了为什么所有ASP.Net网站都运行缓慢的原因,并且我正在尝试解决该问题。

我对这个 Java 代码有一些问题,并且出现错误

我在玩VS2017时发现了一些东西

有人可以帮我解决算法吗?(将权重设置为较大的范围值)

reactjs-我的函数在更新状态和使用Promise时遇到一些问题

在使用 php 更新数据库时,我遇到了一些问题

我在使用 setInterval() 时遇到了一些问题

我在 python 中使用 append 有一些问题

React 不渲染组件 - 我发现了问题,但我不知道如何解决它

我正在使用 discord.py 制作音乐机器人,但在播放命令时遇到了一些问题