如何重复/循环/返回课程 [英] How to repeat/loop/return to a class

查看:66
本文介绍了如何重复/循环/返回课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的初学者,我正在关注YouTube上Thenewboston的教程.我想做一些更高级的事情(我认为……但不是真的).所以这是代码.我要使它重复到开头,这样我就可以输入另一个数字,而无需重新启动程序.我相信我必须使用return命令,但是我不确定在哪里以及如何使用.谢谢!

Im a beginner with java and i am following tutorials by Thenewboston on youtube. I wanted to go to a bit more advanced (in my opinion... but not really) things. So heres the code. I want to make it so that it repeats to the beginning so i can enter another number without restarting the program. I believe i have to use the return command but i am not sure where and how to. Thank you!

import java.util.Scanner;

class apples {

    public static void main(String args[]) {

        Scanner alex = new Scanner(System.in);
        Double test;
        test = alex.nextDouble();

        if (test == 9) {
            System.out.println("eat");
        } else {
            System.out.println("do not eat");
        }
    }
}

推荐答案

import java.util.Scanner;

class apples{

    public static void main(String args[]){

        Scanner alex = new Scanner(System.in);
        Double test;
        while(true) {
            test = alex.nextDouble();
            if (test == 9){
                System.out.println("eat");
            }else{
                System.out.println("do not eat");
            }
        }
    }
}

这篇关于如何重复/循环/返回课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆