0

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. The project is based on the National Car test (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. E.g 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.

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


}

}
4

2 に答える 2

1

これまでのコードに基づいて、ロジックはすべて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

于 2012-11-27T14:08:38.397 に答える
0

まず最初にboolean chkOilLevel()FullTestクラス内などのメソッドを作成します。これにより、次のように、trueまたはfalse評価後に返されます。

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;

このことはまた将来あなたを助けるでしょう...

于 2012-11-27T14:17:41.433 に答える