Java while 循环在我需要的时候不工作
Java Do while loop not working when I need it
为什么我的 output1 打印出来不像更正后的 output1?当数字按升序排列时,程序工作正常。如您所见,当他们不这样做时,它不会。请帮助,我可以使用 do while 或 while 循环。
代码:
import java.util.Scanner;
public class App1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int entry;
int entry2;
int entry3;
System.out.println("Enter three integers");
System.out.println("Side 1: ");
entry = keyboard.nextInt();
System.out.println("Side 2: ");
entry2 = keyboard.nextInt();
do {
entry3 = 0;
if (entry2 <= entry) {
System.out.println(entry2 + " is smaller than " + entry + " Try again.");
System.out.println("Side 2");
entry2 = keyboard.nextInt();
}
else if (entry2 > entry) {
System.out.println("Side 3: ");
entry3 = keyboard.nextInt();
} if (entry3 <= entry2) {
System.out.println("\nYour three sides are " + entry + " " + entry2 + " " + entry3);
System.out.println("NO! These sides do not make a right triangle!");
} if (entry3 > entry2) {
System.out.println("\nYour three sides are " + entry + " " + entry2 + " " + entry3);
System.out.println("These sides *do* make a right triangle. Yippy-skippy!");
}
} while (entry2 < entry);
}
}
我的输出:1
Enter three integers
Side 1:
2
Side 2:
1
1 is smaller than 2 Try again.
Side 2
2
我的输出 2:
Enter three integers
Side 1:
1
Side 2:
2
Side 3:
3
Your three sides are 1 2 3
These sides *do* make a right triangle. Yippy-skippy!
Output1 的正确输出:
Enter three integers:
Side 1: 4
Side 2: 3
3 is smaller than 4. Try again.
Side 2: -9
-9 is smaller than 4. Try again.
Side 2: 5
Side 3: 1
1 is smaller than 5. Try again.
Side 3: 5
Your three sides are 4 5 5
NO! These sides do not make a right triangle!
这是一种方法:
int entry;
int entry2;
int entry3;
System.out.println("Enter three integers");
System.out.println("Side 1: ");
entry = keyboard.nextInt();
System.out.println("Side 2: ");
entry2 = keyboard.nextInt();
while(entry2 <= entry) {
System.out.println(entry2 + " is smaller than " + entry + " Try again.");
System.out.println("Side 2");
entry2 = keyboard.nextInt();
}
//Execution reaches beyond this point only when entry2>entry
System.out.println("Side 3: ");
entry3 = keyboard.nextInt();
if (entry3 <= entry2) {
System.out.println("\nYour three sides are " + entry + " " + entry2 + " " + entry3);
System.out.println("NO! These sides do not make a right triangle!");
}
if (entry3 > entry2) {
System.out.println("\nYour three sides are " + entry + " " + entry2 + " " + entry3);
System.out.println("These sides *do* make a right triangle. Yippy-skippy!");
}
根据你判断三角形是否正确的方法条件,完全错误。如果你是检查3边的长度,那么它应该是这样的:
import java.util.*;
public class App1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int entry;
int entry2;
int entry3;
boolean check = true;
while(check) {
System.out.println("Enter three integers");
System.out.println("Side 1: ");
entry = keyboard.nextInt();
System.out.println("Side 2: ");
entry2 = keyboard.nextInt();
System.out.println("Side 3: ");
entry3 = keyboard.nextInt();
int a = entry*entry;
int b = entry1*entry1;
int c = entry2*entry2;
if((a == b + c) || (b == a + c ) || (c == a + b) {
System.out.println("The Triangle is right.");
check = false;
}
else
System.out.println("The Triangle is not right. Please try Again!!!");
}
}
}
为什么我的 output1 打印出来不像更正后的 output1?当数字按升序排列时,程序工作正常。如您所见,当他们不这样做时,它不会。请帮助,我可以使用 do while 或 while 循环。
代码:
import java.util.Scanner;
public class App1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int entry;
int entry2;
int entry3;
System.out.println("Enter three integers");
System.out.println("Side 1: ");
entry = keyboard.nextInt();
System.out.println("Side 2: ");
entry2 = keyboard.nextInt();
do {
entry3 = 0;
if (entry2 <= entry) {
System.out.println(entry2 + " is smaller than " + entry + " Try again.");
System.out.println("Side 2");
entry2 = keyboard.nextInt();
}
else if (entry2 > entry) {
System.out.println("Side 3: ");
entry3 = keyboard.nextInt();
} if (entry3 <= entry2) {
System.out.println("\nYour three sides are " + entry + " " + entry2 + " " + entry3);
System.out.println("NO! These sides do not make a right triangle!");
} if (entry3 > entry2) {
System.out.println("\nYour three sides are " + entry + " " + entry2 + " " + entry3);
System.out.println("These sides *do* make a right triangle. Yippy-skippy!");
}
} while (entry2 < entry);
}
}
我的输出:1
Enter three integers
Side 1:
2
Side 2:
1
1 is smaller than 2 Try again.
Side 2
2
我的输出 2:
Enter three integers
Side 1:
1
Side 2:
2
Side 3:
3
Your three sides are 1 2 3
These sides *do* make a right triangle. Yippy-skippy!
Output1 的正确输出:
Enter three integers:
Side 1: 4
Side 2: 3
3 is smaller than 4. Try again.
Side 2: -9
-9 is smaller than 4. Try again.
Side 2: 5
Side 3: 1
1 is smaller than 5. Try again.
Side 3: 5
Your three sides are 4 5 5
NO! These sides do not make a right triangle!
这是一种方法:
int entry;
int entry2;
int entry3;
System.out.println("Enter three integers");
System.out.println("Side 1: ");
entry = keyboard.nextInt();
System.out.println("Side 2: ");
entry2 = keyboard.nextInt();
while(entry2 <= entry) {
System.out.println(entry2 + " is smaller than " + entry + " Try again.");
System.out.println("Side 2");
entry2 = keyboard.nextInt();
}
//Execution reaches beyond this point only when entry2>entry
System.out.println("Side 3: ");
entry3 = keyboard.nextInt();
if (entry3 <= entry2) {
System.out.println("\nYour three sides are " + entry + " " + entry2 + " " + entry3);
System.out.println("NO! These sides do not make a right triangle!");
}
if (entry3 > entry2) {
System.out.println("\nYour three sides are " + entry + " " + entry2 + " " + entry3);
System.out.println("These sides *do* make a right triangle. Yippy-skippy!");
}
根据你判断三角形是否正确的方法条件,完全错误。如果你是检查3边的长度,那么它应该是这样的:
import java.util.*;
public class App1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int entry;
int entry2;
int entry3;
boolean check = true;
while(check) {
System.out.println("Enter three integers");
System.out.println("Side 1: ");
entry = keyboard.nextInt();
System.out.println("Side 2: ");
entry2 = keyboard.nextInt();
System.out.println("Side 3: ");
entry3 = keyboard.nextInt();
int a = entry*entry;
int b = entry1*entry1;
int c = entry2*entry2;
if((a == b + c) || (b == a + c ) || (c == a + b) {
System.out.println("The Triangle is right.");
check = false;
}
else
System.out.println("The Triangle is not right. Please try Again!!!");
}
}
}