简体   繁体   English

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

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

I'm currently in my second year of college. 我目前正在大学二年级。 I'm finding the year very hard and don't see myself passing. 我很难过一年,也看不到自己过去。 But any way. 但无论如何。 I'm currently working on a Java project. 我目前正在研究Java项目。 The project is based on the National Car test (NCT). 该项目基于国家汽车测试(NCT)。

Basically what I want to do is have a choice of numbers to carry out a full test or a retest. 基本上我想做的是选择数字来进行全面测试或重新测试。 If the user selects a full test I then want to go to the fullTest class and carry out some question starting with personal information, then car details then car test question. 如果用户选择了完整测试,那么我想进入fullTest类,并从个人信息开始进行一些问题,然后是汽车详细信息,然后是汽车测试问题。 Eg Oil level ok? 例如,油位好吗? Y/N 是/否

What I want to know is how do I go to the fulltest class run through the code and then ether display the results from fulltest class of return the result to mainNct. 我想知道的是如何转到代码中运行的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



}

}

And

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


}

}

Base on the code you have so far, since the logic seems to all reside in your FullTest constructor, this should do it: 根据到目前为止的代码,由于逻辑似乎全部驻留在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

I'm assuming you have a class called ReTest too. 我假设您也有一个称为ReTest的类。

First of all make a method such as boolean chkOilLevel() in FullTest class, that will return you true or false after evaluation as: 首先在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;
}

You can call such method from MainNCT by making an object of FullTest as 您可以通过将MainNCT的对象设置为FullTest来调用此类方法:

FullTest fullTest=new FullTest();

and can call this method in Your MainNCT class 并可以在您的MainNCT类中调用此方法

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

This thing will also help you in future... 这个东西将来也会对您有帮助...

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

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