需要修复以下代码

时间:2017-02-04 07:14:03

标签: java attributes

import java.util.*;


public class LifeSimulator{

    static String name= var;

    public static void Choice1(){

    }


    public static void main(String[] args){

       Scanner input= new Scanner(System.in);

       LifeSimulator player= new LifeSimulator();

       System.out.println("Welcome to Life Simulator 2017! What is your name?");

       player.name= input.nextLine();

       System.out.println("Hello"+name);


}

2 个答案:

答案 0 :(得分:0)

Clear code is as below.

import java.util.*;

public class LifeSimulator {
    static String name;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to Life Simulator 2017! What is your name?");
        LifeSimulator.name = input.nextLine();
        System.out.println("Hello " + name);
        input.close();
    }
}

Corrections
1. Class is not closed with }
2. Invalid variable assignment static String name= var;
3. Unused method Choice1()
4. static field name should be referred with class name LifeSimulator and not by it's object player.

答案 1 :(得分:-1)

以下是您更正后的代码:

import java.util.Scanner;

public class LifeSimulator {

    static String name;

    public static void Choice1() {

    }

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        LifeSimulator player = new LifeSimulator();
        System.out.println("Welcome to Life Simulator 2017! What is your name?");
        player.name = input.nextLine();
        System.out.println("Hello " + name);

    }

}

希望这有帮助。