我在Java中输出null作为输出

时间:2016-12-26 15:13:54

标签: java

我试过阅读其他问题。这似乎是不同的。

所以在Results()方法的下面,我试图在被提名人下方显示选民姓名,但由于某种原因,它在另一名被提名者中显示为空或在下方。

public class Vote{

    String a, b, c, v1, v2, v3, v4, v5;
    int va, vb, vc, r1, r2, r3, r4, r5, r6;

    public void Main_Menu(){
        String n;
        Scanner Menu = new Scanner(System.in);  
        System.out.println("1. Vote");
        System.out.println("2. Credits");
        System.out.println("3. Exit");
        n = Menu.next();
        if (n.equals("1")){
            Voting();
        }
        else if (n.equals("2")){
            System.out.print("Continue");
        }
        else if (n.equals("3")){
            exit();
        }
        else{
            System.out.print("Please input Numbers 1, 2, 3,");
        }
    }

    public void exit(){
        System.exit(0);
    }

    public void Voting(){
        Scanner Nominees = new Scanner(System.in);
        System.out.print("Enter 1st Nominee: ");
        a = Nominees.nextLine();
        System.out.print("Enter 2nd Nominee: ");
        b = Nominees.nextLine();
        System.out.print("Enter 3rd Nominee: ");
        c = Nominees.nextLine();
        System.out.println("Nominees are: " + (a) +", " + (b)+ ", " + (c));
        Confirm();
    }

    public void Confirm(){
        String s;
        Scanner in = new Scanner(System.in);
        System.out.println("Confirm Nomimees ? A.Yes or B.No");
        s = in.next();
        if (s.equals("A")){
            Y();
        }
        else if (s.equals("B")){
             Voting();
        }
        else{
            System.out.println("Please Enter A or B ");
            Confirm();
        }
        in.close();
    }

    public void Y(){
        v1(); 
    }

    public void v1(){
        Scanner usrinp1 = new Scanner(System.in);
        System.out.print("Enter Voters Name: ");
        v1 = usrinp1.nextLine();
        System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
        Scanner in1 = new Scanner(System.in);
        String c1;
        c1 = in1.next();
        if (c1.equals("1")){
            r1++;
            va++;
            v2();
        }
        else if (c1.equals("2")){
            r2++;
            vb++;
            v2();
        }
        else if (c1.equals("3")){
            r3++;
            vc++;
            v2();
        }
        else{            
            System.out.println("Please enter numbers 1, 2, & 3");
            v1();
        }
        in1.close();
    }

    public void v2(){
        Scanner usrinp2 = new Scanner(System.in);
        System.out.print("Enter Voters Name: ");
        v1 = usrinp2.nextLine();
        System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
        Scanner in2 = new Scanner(System.in);
        String c2;
        c2 = in2.next();
        if (c2.equals("1")){
            r4++;
            va++;
            Results();
        }
        else if (c2.equals("2")){
            r5++;
            vb++;
            Results();
        }
        else if (c2.equals("3")){
            r6++;
            vc++;
            Results();
        }
        else{
            System.out.println("Please enter numbers 1, 2, & 3");
            v2();
        }
        in2.close();    
    }

    public void Results(){
        System.out.println((a) + "=" + (va));
        if (r1==1){
            System.out.print(v1);
        }

        if (r4 == 1){
            System.out.print(v2);
        }

        System.out.println("\n"+(b) + "=" + (vb));
        if (r2 == 1){
            System.out.print(v1);
        }
        if (r5 == 1){
            System.out.println(v2);
        }

        System.out.println("\n"+(c) + "=" + (vc));
        if (r3 == 1){
            System.out.println(v1);
        }
        if (r6 == 1){
            System.out.println(v2);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

阅读你的代码非常困难。具有相同名称的方法和变量必然会引起混淆。无论如何:您永远不会为字段v2分配任何值,因此它始终打印为null - 字段在创建时的默认值。

这可能是一个复制粘贴错误,您忘记在粘贴后将v1更改为v2。你最好告诉自己。

下次请查看您是否可以Minimal, Complete, and Verifiable example。我打赌你会在准备好发布问题之前找到错误。 : - )