【问题标题】:If statement loopif语句循环
【发布时间】:2020-08-21 21:23:27
【问题描述】:

所以我制作了一个程序来根据用户的需求来解决不同的数学问题,并且我不希望程序在用户完成后结束。现在,我刚才的循环一遍又一遍地重复相同的选项。想要的效果是能够继续选择你想做的问题。我对编码还很陌生,所以如果这是一个简单的问题,我深表歉意。

public static void main(String[] args)
{
    System.out.println("What problem do you want to do?");
    System.out.println("1:Celcius to Farenheit");
    System.out.println("2:Slope Formula");
    System.out.println("3:Averages");
    System.out.println("4:Perimeter");
    System.out.println("5:Exit\n\n");
    
    Scanner keyboard = new Scanner(System.in);
    
    int response = keyboard.nextInt();

    
    do
    {
    if (response == 1)
    {
        System.out.println("Degrees Celcius:");
        int degrees = keyboard.nextInt();
        System.out.println(degrees + " degrees Celcius is " + celsToFarenheit(degrees) + " degrees Farenheit.");
    }
    else if (response == 2)
    {
        System.out.println("x1:");
        int x1 = keyboard.nextInt();
        System.out.println("y1:");
        int y1 = keyboard.nextInt();
        System.out.println("x2:");
        int x2 = keyboard.nextInt();
        System.out.println("y2:");
        int y2 = keyboard.nextInt();
        System.out.println("The slope is " + slopeFormula(x1,y1,x2,y2));
    }
    else if (response == 3)
    {
        System.out.println("First number:");
        int num1 = keyboard.nextInt();
        System.out.println("Second number:");
        int num2 = keyboard.nextInt();
        System.out.println("The average is " + average(num1,num2));
    }
    else if (response == 4)
    {
        System.out.println("Length:");
        int length = keyboard.nextInt();
        System.out.println("Width:");
        int width = keyboard.nextInt();
        System.out.println("The perimeter is " + perimeter(length,width));
    }
}
while (response != 5);
    

【问题讨论】:

    标签: java loops if-statement


    【解决方案1】:

    像这样改变这一点:

    do
    {
    int response = keyboard.nextInt();
    if (response == 1)
    

    如果您将keyboard.nextInt() 调用移到do 循环内,它现在会反复询问用户输入。

    您在循环之前有这行代码,因此它只处理了一次。

    【讨论】:

      【解决方案2】:

      在循环外声明int response。循环内的地方response = keyboard.nextInt();

      在循环之外使用int response = keyboard.nextInt(),您将永远无法再次到达它。另外,将选项放在循环中,以便它们每次都显示。

      public static void main(String[] args) {
      
      
          Scanner keyboard = new Scanner(System.in);
      
          int response;
      
      
          do {
              System.out.println("What problem do you want to do?");
              System.out.println("1:Celcius to Farenheit");
              System.out.println("2:Slope Formula");
              System.out.println("3:Averages");
              System.out.println("4:Perimeter");
              System.out.println("5:Exit\n\n");
      
              response = keyboard.nextInt();
              if (response == 1) {
                  System.out.println("Degrees Celcius:");
                  int degrees = keyboard.nextInt();
                  System.out.println(degrees + " degrees Celcius is " + celsToFarenheit(degrees) + " degrees Farenheit.");
              } else if (response == 2) {
                  System.out.println("x1:");
                  int x1 = keyboard.nextInt();
                  System.out.println("y1:");
                  int y1 = keyboard.nextInt();
                  System.out.println("x2:");
                  int x2 = keyboard.nextInt();
                  System.out.println("y2:");
                  int y2 = keyboard.nextInt();
                  System.out.println("The slope is " + slopeFormula(x1,y1,x2,y2));
              } else if (response == 3) {
                  System.out.println("First number:");
                  int num1 = keyboard.nextInt();
                  System.out.println("Second number:");
                  int num2 = keyboard.nextInt();
                  System.out.println("The average is " + average(num1,num2));
              } else if (response == 4) {
                  System.out.println("Length:");
                  int length = keyboard.nextInt();
                  System.out.println("Width:");
                  int width = keyboard.nextInt();
                  System.out.println("The perimeter is " + perimeter(length,width));
              }
          }
          while (response != 5);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        • 2017-02-17
        相关资源
        最近更新 更多