【问题标题】:Trouble with loop and storing data in variables循环问题和将数据存储在变量中
【发布时间】:2026-02-07 04:55:01
【问题描述】:

我试图完成的程序需要从用户那里读取三个数字,然后将它们存储在变量num1num2num3 中,以便它们可以在步骤 2 和 3 中使用。我遇到的问题是,当程序在用户输入他们的数字并尝试继续执行第 2 步或第 3 步后循环时,它说必须先完成第 1 步。
这是我现在的代码:

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int num1;
        int num2;
        int num3;
        int input;

        do {
            boolean opt1Done = false;
            System.out.println("Select your next step");
            System.out.println("1: Enter three numbers between 1 and 100.");
            System.out.println("2: Order your number in ascending order");
            System.out.println("3: Determine if the three inputs form a triangle");
            System.out.println("4: Exit");

            int answer = console.nextInt();
            num1 = console.nextInt();
            num2 = console.nextInt();
            num3 = console.nextInt();
            input = console.nextInt();

            if (answer == 1) {
                //do whatever for option 1
                System.out.println("Enter a value for num1 between 1 and 100.");
                System.out.println("Enter a value for num2 between 1 and 100.");
                System.out.println("Enter a value for num3 between 1 and 100.");

                opt1Done = true;
            } else if (answer == 2) {
                if (opt1Done) {
                    //...... do whatever to order the numbers
                    int[] arraynum;
                    arraynum = new int[3];

                    arraynum[0] = num1;
                    arraynum[1] = num2;
                    arraynum[2] = num3;

                    Arrays.sort(arraynum);

                    int i;

                    for (i = 0; i < arraynum.length; i++) {
                        System.out.println("num:" + arraynum[i]);
                    }

                } else {
                    System.out.println("you must complete Step 1 before Step 2");
                }
            } else if (answer == 3) {
                if (opt1Done) {
                    //... do whatever to determine if triangle or not
                    if (num1 + num2 > num3 && num1 + num3 > num2 && num2 + num3 > num1) {
                        System.out.print("TRIANGLE");
                    } else {
                        System.out.print("NO TRIANGLE");
                    }
                } else {
                    System.out.println("you must complete Step 1 before Step 3");
                }
                if (answer == 4) {
                    System.exit(0);

                }
            }
        } while (input != 4);
    }
}

我需要添加另一个循环还是更改已有的循环?

【问题讨论】:

  • 似乎是它的功课。是吗?
  • 它用于编程课程的项目是的@MuhammadSuleman
  • 你又破解了密码!编辑问题时要小心;)
  • opt1Done 在每次迭代后被设置为 false,在你开始做之前移动 boolean opt1Done = false; ..while 循环

标签: java loops variables storing-data


【解决方案1】:

将布尔声明 opt1Done 和数字移到循环之外,并在每次用户提示后读取输入。

 import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;

public class ReadChar {

    public ReadChar() {
        // TODO Auto-generated constructor stub
    }

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int num1 = 0;
        int num2 = 0;
        int num3 = 0;
        boolean opt1Done = false;

        while (true) {

            System.out.println("Select your next step");
            System.out.println("1: Enter three numbers between 1 and 100.");
            System.out.println("2: Order your number in ascending order");
            System.out
                    .println("3: Determine if the three inputs form a triangle");
            System.out.println("4: Exit");

            int answer = console.nextInt();

            if (answer == 1) {
                // do whatever for option 1
                System.out.println("Enter a value for num1 between 1 and 100.");
                num1 = console.nextInt();

                System.out.println("Enter a value for num2 between 1 and 100.");
                num2 = console.nextInt();
                System.out.println("Enter a value for num3 between 1 and 100.");
                num3 = console.nextInt();

                opt1Done = true;
            } else if (answer == 2) {
                if (opt1Done) {
                    // ...... do whatever to order the numbers
                    int[] arraynum;
                    arraynum = new int[3];

                    arraynum[0] = num1;
                    arraynum[1] = num2;
                    arraynum[2] = num3;

                    Arrays.sort(arraynum);

                    int i;

                    for (i = 0; i < arraynum.length; i++) {
                        System.out.println("num:" + arraynum[i]);
                    }

                } else {
                    System.out
                            .println("you must complete Step 1 before Step 2");
                }
            } else if (answer == 3) {
                if (opt1Done) {
                    // ... do whatever to determine if triangle or not
                    if (num1 + num2 > num3 && num1 + num3 > num2
                            && num2 + num3 > num1) {
                        System.out.print("TRIANGLE");
                    } else {
                        System.out.print("NO TRIANGLE");
                    }

                } else {
                    System.out
                            .println("you must complete Step 1 before Step 3");
                }

            }
            if (answer == 4) {
                System.exit(0);

            }
        }
    }
}

【讨论】:

    【解决方案2】:

    您不会循环查看说明。每次你完成一个步骤,程序就会结束,下次你运行它时,你之前所做的一切都不会被保存。

    您需要一个外部循环来迭代命令:

    public static void main(String[] args) {
    
        int num1;
        int num2;
        int num3;
    
        boolean opt1Done = false;
    
        while (true) {
    
            System.out.println("Select your next step");
            System.out.println("1: Enter three numbers between 1 and 100.");
            System.out.println("2: Order your number in ascending order");
            System.out.println("3: Determine if the three inputs form a triangle");
            System.out.println("4: Exit");
    
            //rest of your code here
        }
    }
    

    【讨论】:

      【解决方案3】:

      您似乎必须循环直到用户选择选项 4。我没有看到任何包含问题选项的循环。

      【讨论】:

        【解决方案4】:

        希望对你有帮助

        • 引入了一个 while 循环,并且 opt1True 应该在循环之外
        • 检查答案 4 在答案 3 中。因此,如果与其他答案检查处于同一水平,则将其移入 else 中

          static Scanner console = new Scanner(System.in);
          
              public static void main(String[] args) {
                  // TODO Auto-generated method stub
          
                  int num1 = 0;
                  int num2 = 0;
                  int num3 = 0;
                  boolean opt1Done = false;
                  while(true) {
                  System.out.println("Select your next step");
                  System.out.println("1: Enter three numbers between 1 and 100.");
                  System.out.println("2: Order your number in ascending order");
                  System.out.println("3: Determine if the three inputs form a triangle");
                  System.out.println("4: Exit");
          
                  int answer = console.nextInt();
                  if (answer == 1) {
                      // do whatever for option 1
                      System.out.println("Enter a value for num1 between 1 and 100.");
                      num1 = console.nextInt();
          
                      System.out.println("Enter a value for num2 between 1 and 100.");
                      num2 = console.nextInt();
          
                      System.out.println("Enter a value for num3 between 1 and 100.");
                      num3 = console.nextInt();
          
                      opt1Done = true;
                  } else if (answer == 2) {
                      if (opt1Done) {
                          // ...... do whatever to order the numbers
                          int[] arraynum;
                          arraynum = new int[3];
          
                          arraynum[0] = num1;
                          arraynum[1] = num2;
                          arraynum[2] = num3;
          
                          Arrays.sort(arraynum);
          
                          int i;
          
                          for (i = 0; i < arraynum.length; i++) {
                              System.out.println("num:" + arraynum[i]);
                          }
          
                      } else {
                          System.out.println("you must complete Step 1 before Step 2");
                      }
                  } else if (answer == 3) {
                      if (opt1Done) {
                          // ... do whatever to determine if triangle or not
                          if (num1 + num2 > num3 && num1 + num3 > num2
                                  && num2 + num3 > num1) {
                              System.out.print("TRIANGLE");
                          } else {
                              System.out.print("NO TRIANGLE");
                          }
                      } else {
                          System.out.println("you must complete Step 1 before Step 3");
                      }
          
                  } else if (answer == 4) {
                      System.exit(0);
          
                  }
          
                  }
          
              }
          

        【讨论】:

          【解决方案5】:

          你把条件放在错误的地方。 因为我在想首先检查用户的答案,然后将第一个条件中的这 3 个数字作为

          int answer = console.nextInt();
              if (answer == 1) {
                  //do whatever for option 1
                  System.out.println("Enter a value for num1 between 1 and 100.");
                  num1 = console.nextInt();
                  System.out.println("Enter a value for num2 between 1 and 100.");
                  num2 = console.nextInt();
                  System.out.println("Enter a value for num3 between 1 and 100.");
                  num3 = console.nextInt();
                  opt1Done = true;
             }
          

          并将这条线移到循环之外

           boolean opt1Done = false;
          

          【讨论】:

            【解决方案6】:

            您所要做的就是在逻辑中添加一个 while 循环并重新排序。

                boolean opt1Done = false;
                int num1, num2, num3;
              while(answer != 4){
            
            
              System.out.println("Select your next step");
                System.out.println("1: Enter three numbers between 1 and 100.");
                System.out.println("2: Order your number in ascending order");
                System.out.println("3: Determine if the three inputs form a triangle");
                System.out.println("4: Exit");
            
                int answer = console.nextInt();
            
            
                if (answer == 1) {
                    //do whatever for option 1
                    System.out.println("Enter a value for num1 between 1 and 100.");
                    num1 = console.nextInt();
                    System.out.println("Enter a value for num2 between 1 and 100.");
                    num2 = console.nextInt();
                    System.out.println("Enter a value for num3 between 1 and 100.");
                    num3 = console.nextInt();
                opt1Done = true;
              }
            
                                                ...
            
            
                   if (answer == 4) {
                                System.exit(0);
            
                            }
                        }
                     }
                 }
            

            您的问题是每次循环时,您都在重新声明布尔变量并将其设置为 false。再加上你的 console.nextInts() 是在错误的位置。在这种情况下也不需要做while,因为answer的起始值为null,因此不等于4。

            【讨论】:

              最近更新 更多