小编use*_*447的帖子

如何在getter和setter中使用Enums?

所以我想要做的是:

写一个User类

用户:

  • 有一个用户名,例如'fj3'
  • 有一个userType,可以是:'user','editor'或'admin'
  • 有一个名字,例如'弗朗西斯'
  • 有一个构造函数,它将username,userType和name作为参数
  • 有一个getUsername()方法
  • 有一个getUserType()方法
  • 有一个getName()方法
  • 有一个setUserType()方法,它将一个用户类型作为参数

我的代码到目前为止:

public class User{

     public String id;
     public String userPermissions;
     public String actualName;

     public User(String username, String userType, String name){
         id = username;
         userPermissions = userType;
         actualName= name;
     }

    public String getUsername(){
        return id;
    }

    public String getUserType(){
        return userPermissions;
    }       

    public String getName(){
        return actualName;
    }

    public enum UserType{
       admin, editor, user;
    }

    public void setUserType(String input){
        userPermissions = input;
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要做些什么才能让它发挥作用?我不知道如何制作它所以可以选择的唯一用户类型是管理员,编辑者或用户.

java enums

11
推荐指数
1
解决办法
3万
查看次数

我尝试运行此程序时导致java.lang.NullPointerException错误的原因是什么?

我的代码是这样的:

public class Main{

    public static void main(String[] args){
        WordGroup wordgroupOne= new WordGroup ("You can discover more about a person in an hour of play than in a year of conversation");
        WordGroup wordgroupTwo= new WordGroup ( "When you play play hard when you work dont play at all");

        String[] quoteOne = wordgroupOne.getWordArray();   
        String[] quoteTwo = wordgroupTwo.getWordArray();

        for (String word : quoteOne){
            System.out.println(word);
        }

        for (String word : quoteTwo){                       
            System.out.println(word);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Wordgroup类:

public class WordGroup {
    public String words; …
Run Code Online (Sandbox Code Playgroud)

java arrays

4
推荐指数
1
解决办法
312
查看次数

我如何将此代码转换为循环?

public Bee anotherDay(){
    flower = garden.findFlower();
    int pol = 5;
    bool=flower.extractPollen(pol);
    if(bool=true){  
        hive.addPollen(pol);
    }else{
        ++pol;
        bool=flower.extractPollen(pol);

        if(bool=true){  
            hive.addPollen(pol);
        }else{
            ++pol; //etc.
        } 
    }
Run Code Online (Sandbox Code Playgroud)

该代码的重点是:

1)use the findFlower() method on garden ot return a flower
2)use the extract pollen method on the flower with 5 as the initial paramater
3)If there isn't 5 pollen in the flower, the method returns false so try again with 4
4)If there isn't 4 try with 3 etc. until 0. 
Run Code Online (Sandbox Code Playgroud)

我正在考虑使用for循环,但我不知道如果方法成功并且返回true,如何突破它,所以我不会继续从花中获得5 + 4 + 3 …

java loops if-statement

0
推荐指数
1
解决办法
55
查看次数

标签 统计

java ×3

arrays ×1

enums ×1

if-statement ×1

loops ×1