java java中如何返回到特定的代码行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26287137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:39:01  来源:igfitidea点击:

How to return to a specific line of code in java

javarepeat

提问by Warren Manuel

OK so this is my first post here. (Be kind to me :)) I'm like two weeks old to java and wanted to create an app that can do the jobs of a total, average and average grader. My question here is. How can I return to a specific line of code after one operation has ended Eg: I want the code to runString menu1="Input the number of the operation you desire "; after the total or average has been found. Thanks in advance

好的,这是我在这里的第一篇文章。(善待我 :)) 我就像两周大的 Java 一样,想创建一个应用程序,可以完成总分、平均分和平均分的工作。我的问题是。一个操作结束后如何返回到特定的代码行 例如:我希望代码运行字符串 menu1="输入您想要的操作编号"; 在找到总数或平均值之后。提前致谢

import java.util.Scanner;
public class grader
{
    public static void main (String args[])
    {
    Scanner input=new Scanner(System.in);
    System.out.println ("Input your Chemistry marks");
    float a= input.nextFloat();

    System.out.println ("Input your Biology marks");
    float b= input.nextFloat();

    System.out.println ("Input your Physics marks");
    float c= input.nextFloat();

    String menu1="Input the number of the operation you desire ";
    String op1="1. Total ";
    String op2="2. Average ";
    String op3="3. Grade ";
    String op4="4. Exit ";
    System.out.println (menu1+op1+op2+op3+op4);

    Scanner menuselect=new Scanner(System.in);
    int option= input.nextInt();

            float tot=(a+b+c);
            float avg=((a+b+c)/3);
    if (option==1)
        System.out.println ("Your total is "+tot);

    else if (option==2)
        System.out.println ("Your average is "+avg);

    else if (option==3)
        if (avg<0.0)
            System.out.println ("Please input proper data");
        else if (avg<=49.9)
            System.out.println ("Fail");
        else if (avg<=69.9)
            System.out.println ("Pass");
        else if (avg<=100.0)
            System.out.println ("Excellent");
        else 
            System.out.println ("Please input proper data");
    }
}

回答by Epiglottal Axolotl

You should use the do-whileloop. How it works is it executes some code, checks to see if a condition evaluates to true, and if so, executes the code again.

你应该使用do-while循环。它的工作原理是执行一些代码,检查条件是否计算为true,如果是,则再次执行代码。

do {
    //your code here
}
while (/*some condition*/);

回答by Scraniel

Usually when you want to repeat one or more lines of code, you do so using a loop. In general, a loop is set up like this:

通常,当您想重复一行或多行代码时,您可以使用循环。一般来说,一个循环是这样设置的:

while(loopGuard){

    //code to repeat

}

Where loopGuardis some boolean statement that gets updated inside your loop. The code inside the loop will continue executing until your loopGuardstatement is no longer true.

loopGuard循环中更新的布尔语句在哪里。循环内的代码将继续执行,直到您的loopGuard语句不再为真。

In your case, you may want to loop until the user presses a certain button, until it has run a certain number of times, or any other arbitrary reason you have chosen. The main thing to remember is to make sure that your loop guard will eventually become false, otherwise you'll find yourself in an infinite loop.

在您的情况下,您可能想要循环直到用户按下某个按钮,直到它运行了一定次数,或者您选择的任何其他任意原因。要记住的主要事情是确保你的循环守卫最终会变成假的,否则你会发现自己处于无限循环中。

回答by Naili

In your case you should use a whileloop with the usage of continueand break. A second option, you can use a labeled breakstatement (explained here).

在您的情况下,您应该使用while循环使用continueand break。第二种选择,您可以使用带标签的break语句(在此处解释)。

Btw, in java language there a gotokeyword. But it's not implemented (or removed). Read herefor more details.

顺便说一句,在java语言中有一个goto关键字。但它没有实现(或删除)。阅读此处了解更多详情。

回答by Rohan Paul

I would suggest a do - while loop . A do while loop runs at least once , which is necessary as you're program needs to be run at least once . Put all the statements from Input the operation number to the calculating statements under do while loop . It should work out just fine .

我建议使用 do-while 循环。do while 循环至少运行一次,这是必要的,因为您的程序需要至少运行一次。将输入操作数中的所有语句放入 do while 循环下的计算语句中。它应该工作得很好。