我的循环不会让我输入用户输入来结束循环:((java)

时间:2013-10-14 03:44:22

标签: java class loops methods

好的,基本上,我将所有方法都联系起来,一切都很好看(通过我的小眼睛)。我有一个循环并询问用户是否想要继续。我最初有一个基于y / n输入的if else语句。如果输入n,我会休息一下;声明。但是,我在我评论的行中初始化扫描仪和print语句//这不起作用。一旦我打字,为什么不让我输入一些东西呢?

主类


import java.util.Scanner;
public class Triangle
{
    public static void main (String[]args)  
    {
        Scanner input = new Scanner(System.in);
        int x, y, z;
        for (int loop = 1; loop <= Integer.MAX_VALUE; loop++)
        {
            System.out.println("Please enter the 3 sides of the triangle.");
            x = input.nextInt();
            y = input.nextInt();
            z = input.nextInt();
            boolean goAgain;
            String userInput;

            TriMethods triTest = new TriMethods(x, y, z);

            System.out.println("You entered a triangle with the sides " + triTest.toString() + ": ");

            triTest.isEqual();
            triTest.isIsos();
            triTest.isScal();

            System.out.println("Would you like to go again? y/n");

            String answer = userInput.nextLine(); //THIS DOESN'T WORK

        }
    }
}

第二课


public class TriMethods {

    int side1;
    int side2;
    int side3;

    public TriMethods(int x, int y, int z)
    {
        side1 = x;
        side2 = y;
        side3 = z;
    }

    public int longSide()
    {
        int longSide = side1;

        if (side2 >= longSide && side2 >= side3)
        {
            longSide = side2;
        }
        else if (side3 >= side2 && side3 >= side2)
        {
            longSide = side3;
        }
        return longSide;
    }

    public void shortSide()
    {
        int shortSide = side1;

        if (side2 <= shortSide && side2 <= side3)
        {
            shortSide = side2;
        }
        else if (side3 <= side2 && side3 <= side2)
        {
            shortSide = side3;
        }

        System.out.println("The long side is " + shortSide);
    }

    public void isEqual()
    {
        if (side1 == side2 && side1 == side3 && side2 == side3)
        {
            System.out.println("This triangle is equalateral");     
        }
    }

    public void isIsos()
    {
        if (side1 == side2 && side1 != side3)
        {
            System.out.println("This triangle is isosceles");
        }
        else if (side1 == side3 && side1 != side2)
        {
            System.out.println("This triangle is isosceles");
        }
        else if (side2 == side1 && side2 != side3)
        {
            System.out.println("This triangle is isosceles");
        }
        else if (side2 == side3 && side2 != side1)
        {
            System.out.println("This triangle is isosceles");
        }
    }

    public void isScal()
    {
        if (side1 != side2 && side1 != side3 && side2 != side3)
        {
            System.out.println("This triangle is scalene");
        }
    }

    public String toString()
    {
        return (side1 + " " + side2 + " " + side3);
    }

}

2 个答案:

答案 0 :(得分:3)

在获得三角形边后尝试使用input.nextLine();,按Enter键时会消耗新行。

public class Triangle
{
    public static void main (String[]args)

    {

        int x, y, z;
        String proceed="";
        do
        {
             Scanner input = new Scanner(System.in);
        System.out.println("Please enter the 3 sides of the triangle.");

        x = input.nextInt();
        y = input.nextInt();
        z = input.nextInt();
        boolean goAgain;
        String userInput;

        TriMethods triTest = new TriMethods(x, y, z);

        System.out.println("You entered a triangle with the sides " + triTest.toString() + ": ");

        triTest.isEqual();
        triTest.isIsos();
        triTest.isScal();
        input.nextLine();
        System.out.println("Would you like to go again? y/n");

        proceed = input.nextLine(); //THIS DOESN'T WORK

        }while (!proceed.equalsIgnoreCase("n"));
    }
}

答案 1 :(得分:1)

基本上你的字符串userInput没有值。您可能希望使用Scanner

获取价值