我想要完成的程序需要读取用户的三个数字,然后将它们存储在变量num1
,num2
和num3
中,以便它们可以用于第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);
}
}
我是否需要添加另一个循环或更改已有的循环?
答案 0 :(得分:0)
您不会循环说明。每当你完成一个步骤,程序就会结束,下次你运行它,之前你所做的一切都没有保存。
你需要一个外部循环来迭代命令:
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
}
}
答案 1 :(得分:0)
似乎你必须循环直到用户选择选项4.我没有看到任何包含选项提问的循环。
答案 2 :(得分:0)
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;
答案 3 :(得分:0)
在每个用户提示后移动布尔声明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);
}
}
}
}
答案 4 :(得分:0)
希望这有帮助
检查答案4是否在答案3内。所以如果与其他答案检查处于同一水平那么移动
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 :(得分:0)
您所要做的就是为您的逻辑添加一个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。