2

好的,所以基本上,我把我所有的方法都联系起来了,一切看起来都很好(通过我的笨拙的眼睛)。我有一个循环并询问用户是否要继续。我最初有一个基于 ay/n 输入的 if else 语句。如果输入 n,我使用 break;陈述。但是,我在注释//这不起作用的行中初始化了扫描仪和打印语句。为什么一旦我输入它就不允许我输入一些东西?

主班


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

}
4

2 回答 2

3

input.nextLine();在得到三角形的边后尝试使用,当你按下回车键时这将消耗新的线。

尝试

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"));
    }
}
于 2013-10-14T03:55:18.813 回答
1

基本上你的字符串userInput没有值。可能你想通过使用来获得价值Scanner

于 2013-10-14T03:47:41.520 回答