Java当我需要时,循环不工作

时间:2015-08-28 11:59:56

标签: java while-loop do-while

为什么我的输出1不像修正后的输出1那样打印出来?当数字按升序排列时,程序运行正常。当他们没有你可以看到它没有。请帮忙,我可以使用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!

2 个答案:

答案 0 :(得分:1)

这是一种方法:

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

答案 1 :(得分:0)

根据您检查三角形是否正确的接近条件,这是完全错误的。如果要检查3面的长度,则应如下所示:

.sub-menu {display: none;}
li.menu-item-has-children:hover > .sub-menu {display: block;}