繁体   English   中英

如何去另一个班级执行代码并返回上一个班级

[英]How to go to a different class carry out code and return to previous class

我目前正在大学二年级。 我很难过一年,也看不到自己过去。 但无论如何。 我目前正在研究Java项目。 该项目基于国家汽车测试(NCT)。

基本上我想做的是选择数字来进行全面测试或重新测试。 如果用户选择了完整测试,那么我想进入fullTest类,并从个人信息开始进行一些问题,然后是汽车详细信息,然后是汽车测试问题。 例如,油位好吗? 是/否

我想知道的是如何转到代码中运行的fulltest类,然后以太显示来自fulltest类的结果,并将结果返回给mainNct。

package Nct;


import java.util.Scanner;

public class MainNCT 
{

public static int choice = -1;

public static void main( String[] args) 
{

    Scanner Console = new Scanner(System.in);


    System.out.println("Menu\n\t1. Full Test\n\t2. Re-test\n\t0. Exit\n");

    System.out.print("Enter a number: ");
    choice = Console.nextInt();

    switch(choice)
    {
        case 1:
            //Go to fulltest class
            break;
        case 2:
            //Go to retest class
            break;
        case 0:
            System.exit(0);
            break;
        default:
            System.out.println("Invalid number entered");
    } // switch



}

}

package Nct;

import java.util.Scanner;

public class FullTest extends MainNCT {

int wheelAliResult = 0;
String wheelResult;

public FullTest() {

    Scanner Console = new Scanner(System.in);
    //Questions here

    System.out.print("Wheel alingment (%)? ");
    wheelAliResult = Console.nextInt();

    if(wheelAliResult < 0 || wheelAliResult > 6)
    {
        wheelResult = "Fail";
    }
    else
    {
        wheelResult = "Pass";
    }


}

}

根据到目前为止的代码,由于逻辑似乎全部驻留在FullTest构造函数中,因此应该这样做:

switch(choice)
{
    case 1:
        FullTest ft = new FullTest();
        break;
    case 2:
        ReTest rt = new ReTest();
        break;
    case 0:
        System.exit(0);
        break;
    default:
        System.out.println("Invalid number entered");
} // switch

我假设您也有一个称为ReTest的类。

首先在FullTest类中FullTest诸如boolean chkOilLevel() FullTest类的方法,该方法在评估为以下内容后将返回truefalse

boolean chkOilLevel(){
Scanner Console = new Scanner(System.in);
//Questions here

System.out.print("Wheel alingment (%)? ");
wheelAliResult = Console.nextInt();

if(wheelAliResult < 0 || wheelAliResult > 6)
{
    wheelResult = true;
}
else
{
    wheelResult = false;
}
return wheelResult;
}

您可以通过将MainNCT的对象设置为FullTest来调用此类方法:

FullTest fullTest=new FullTest();

并可以在您的MainNCT类中调用此方法

        case 1:
           boolean oilLevel=fullTest.chkOilLevel();
           // Do Whatever you want with oilLevel
           break;

这个东西将来也会对您有帮助...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM