简单的循环,初学者

时间:2013-12-12 20:51:35

标签: java loops for-loop

当您看到我的问题时,您可以看到

Java新手。 我想:

  • 使用转换菜单提示用户
  • 例如,如果他们选择5,请他们输入转换为公斤
  • 继续给他们菜单,直到他们输入7退出

我的代码存在问题   - 无论最初命中的是什么号码,都不会出现上面的步骤2。它仅在输入第二个数字后出现   - 当7输入时它实际上没有退出

我也是这个网站的新手,所以如果我没有正确地提出这个问题我会道歉

这是代码

class Test
{
    public static void main(String[] args)
    {
        int number;
        int index;
        int input;
        Double conversion;

        index = 1;

        System.out.println("Enter 1 for Fahrenheit to Celius ");
        System.out.println("Enter 2 for Celius to Fahrenheit ");
        System.out.println("Enter 3 for Inches to Centimetres ");
        System.out.println("Enter 4 for Centimetres to Inches ");
        System.out.println("Enter 5 for Pounds to Kg ");
        System.out.println("Enter 6 for Kg to Pounds ");
        System.out.println("Enter 7 to quit ");
        number = EasyIn.getInt();

        for (index=1; index <=6; index++)

        {
            System.out.println("Enter 1 for Fahrenheit to Celsius ");
            System.out.println("Enter 2 for Celsius to Fahrenheit ");
            System.out.println("Enter 3 for Inches to Centimetres ");
            System.out.println("Enter 4 for Centimetres to Inches ");
            System.out.println("Enter 5 for Pounds to Kg ");
            System.out.println("Enter 6 for Kg to Pounds ");
            System.out.println("Enter 7 to quit ");
            number = EasyIn.getInt();

        if (number == 1)
            {
                System.out.print("Input an amount to convert to Celsius ");
                input = EasyIn.getInt();
                conversion = (5.0/9.0) * (input - 32);
                System.out.println("Your input to Celsius is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 2)
            {
                System.out.print("Input an amount to convert to Fahrenheit ");
                input = EasyIn.getInt();
                conversion = input * 2.8;
                System.out.println("Your input to Fahrenheit is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 3)
            {
                System.out.print("Input an amount to convert to Centimetres ");
                input = EasyIn.getInt();
                conversion = input * 2.54;
                System.out.println("Your input to Centimtres is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 4)
            {
                System.out.print("Input an amount to convert to Inches ");
                input = EasyIn.getInt();
                conversion = input * 0.393701;
                System.out.println("Your input to Inches is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 5)
            {
                System.out.print("Input an amount to convert to Kg ");
                input = EasyIn.getInt();
                conversion = input * 0.453592;
                System.out.println("Your input to Kgs is " + conversion);
                System.out.println("Press 7 to quit");
            }

        if (number == 6)
            {
                System.out.print("Input an amount to convert to Pounds ");
                input = EasyIn.getInt();
                conversion = input * 2.20462;
                System.out.println("Your input to pounds is " + conversion);
                System.out.println("Press 7 to quit");
            }
        }
    }
}

7 个答案:

答案 0 :(得分:3)

System.out.print("Input an amount to convert to Celsius ");

在显式刷新System.out流或打印换行符之前,此输出将无法到达您的显示。在接下来发生之前,您会继续期望用户输入,因此您没有看到提示。

这是因为通常情况下,输出流是缓冲的,以便在通过它们发送大量数据时优化性能。 PrintStream具有 autoflush 行为,这使得此问题在大多数情况下都不可见。它通过刷新每个行尾标记来工作,您不会使用print发送。

答案 1 :(得分:1)

您在循环之前设置number ,然后立即进入循环。这就是它忽略你的第一个价值的原因。删除

number = EasyIn.getInt();
在for循环之前

它也不会退出,因为你没有告诉它在输入7时该怎么做。对于那种情况,你需要一个新的if块:

if(number == 7) {
    //bla bla
    break;
}

答案 2 :(得分:0)

public static void main(String[] args){        
    int number;
    int index;
    int input;
    Double conversion;

    do{

    System.out.println("Enter 1 for Fahrenheit to Celius ");
    System.out.println("Enter 2 for Celius to Fahrenheit ");
    System.out.println("Enter 3 for Inches to Centimetres ");
    System.out.println("Enter 4 for Centimetres to Inches ");
    System.out.println("Enter 5 for Pounds to Kg ");
    System.out.println("Enter 6 for Kg to Pounds ");
    System.out.println("Enter 7 to quit ");
    number = EasyIn.getInt();


    if (number == 1)
        {
            System.out.print("Input an amount to convert to Celsius ");
            input = EasyIn.getInt();
            conversion = (5.0/9.0) * (input - 32);
            System.out.println("Your input to Celsius is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 2)
        {
            System.out.print("Input an amount to convert to Fahrenheit ");
            input = EasyIn.getInt();
            conversion = input * 2.8;
            System.out.println("Your input to Fahrenheit is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 3)
        {
            System.out.print("Input an amount to convert to Centimetres ");
            input = EasyIn.getInt();
            conversion = input * 2.54;
            System.out.println("Your input to Centimtres is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 4)
        {
            System.out.print("Input an amount to convert to Inches ");
            input = EasyIn.getInt();
            conversion = input * 0.393701;
            System.out.println("Your input to Inches is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 5)
        {
            System.out.print("Input an amount to convert to Kg ");
            input = EasyIn.getInt();
            conversion = input * 0.453592;
            System.out.println("Your input to Kgs is " + conversion);
            System.out.println("Press 7 to quit");
        }

    if (number == 6)
        {
            System.out.print("Input an amount to convert to Pounds ");
            input = EasyIn.getInt();
            conversion = input * 2.20462;
            System.out.println("Your input to pounds is " + conversion);
            System.out.println("Press 7 to quit");
        }
    }while(number>=1 && number<7);


}

答案 3 :(得分:0)

我看到的一些事情。首先,在For循环之外提示。然后你在For循环中再次提示。这就是它忽略第一个数字的原因。摆脱所有那些System.out.print调用和For循环外的GetInt()。

接下来,为什么使用带索引的For循环?你的代码只会循环6次然后就会结束。每次。不多也不少。

将其切换为一段时间(true)。

让七人辞职。

添加此

if(number == 7)
{
    break;
}

答案 4 :(得分:0)

您在四个循环中使用索引,您可以像这样修改循环:

while (number != 7) {
    // ...
}

您在第一次迭代中显示两次菜单。只有一个在循环之前,另一个在循环中。您可以删除在循环之前显示菜单的代码,并初始化循环的值或使用do-while bucle。

do {
    // Show menu code
    // Read user action
    // Selected action code
} while (number != 7)

答案 5 :(得分:0)

而不是我纠正你的答案,这是你的目标的更好的解决方案:

使用while循环代替for循环。条件是输入 7。

经过测试并正常工作

int index = 0;
double conversion = 0;
int input = 0;

System.out.println("Enter 1 for Fahrenheit to Celius ");
System.out.println("Enter 2 for Celius to Fahrenheit ");
System.out.println("Enter 3 for Inches to Centimetres ");
System.out.println("Enter 4 for Centimetres to Inches ");
System.out.println("Enter 5 for Pounds to Kg ");
System.out.println("Enter 6 for Kg to Pounds ");
System.out.println("Enter 7 to quit ");

Scanner in = new Scanner(System.in);

index = in.nextInt();

while (index != 7)
{
    if (index == 1)
    {
        System.out.print("Input an amount to convert to Celsius ");
        input = in.nextInt();
        conversion = (5.0/9.0) * (input - 32);
        System.out.println("Your input to Celsius is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 2)
    {
        System.out.print("Input an amount to convert to Fahrenheit ");
        input = in.nextInt();
        conversion = input * 2.8;
        System.out.println("Your input to Fahrenheit is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 3)
    {
        System.out.print("Input an amount to convert to Centimetres ");
        input = in.nextInt();
        conversion = input * 2.54;
        System.out.println("Your input to Centimtres is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 4)
    {
        System.out.print("Input an amount to convert to Inches ");
        input = in.nextInt();
        conversion = input * 0.393701;
        System.out.println("Your input to Inches is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 5)
    {
        System.out.print("Input an amount to convert to Kg ");
        input = in.nextInt();
        conversion = input * 0.453592;
        System.out.println("Your input to Kgs is " + conversion);
        System.out.println("Press 7 to quit");
    }

    if (index == 6)
    {
        System.out.print("Input an amount to convert to Pounds ");
        input = in.nextInt();
        conversion = input * 2.20462;
        System.out.println("Your input to pounds is " + conversion);
        System.out.println("Press 7 to quit");
    }
    index = in.nextInt();
}

答案 6 :(得分:0)

如果我正确理解了这个问题,问题就在于你正在调用

number = EasyIn.getInt();
for for循环之前

,然后在for循环中再次调用它 。您第一次调用EasyIn.getInt()时实际上并没有进行任何处理,因此在第二次调用之前您没有看到任何结果。

按7不会导致循环退出,因为一旦输入就不会处理“7”。

我想你想要更像这样的东西:

while (true) {
    //do all your System.out.print() stuff
    number = EasyIn.getInt();

    // if conditions for 1, 2, 3, 4, 5, and 6

    if (number == 7) {
        return;
    }
}