如何设置变量的最大值?

时间:2018-03-18 11:48:10

标签: java netbeans int limit

如何设置一个最大数量,以便某个变量不能超过我的代码中的限制,玩家可以反复进行治疗,因此他们的健康状况会超过他们的最大HP

public class HEAL {
    public static int maxhp = 25;
    public static int hp = 10;

    public static void main(String[] args) throws IOException {
        heal();
    }

    public static void heal() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Would you like me to heal you? \n[1] Yes\n[2]No");
        int choice = Integer.parseInt(br.readLine());
        if (choice == 1) { 
            System.out.println("You have been healed for 10 hp");
            HEAL.hp = HEAL.hp + 10;
            System.out.println("Your current HP: "+HEAL.hp);
        }
        else if (choice == 2) {
            System.out.println("Leave");
        } 
    }
}

6 个答案:

答案 0 :(得分:1)

我要做的是创建一个函数setHp,它将检查它是否在约束范围内。

if (val < 0 || val > 100) {
     /* not within constraints, do what you want here */
  } else {
  hp = val;
}

答案 1 :(得分:1)

首先,使变量hp为private和2nd,在对象内部处理if和何时递增。 这个

[1]

HEAL.hp = Math.min(HEAL.hp + 10, 25)

[2]

25

答案 2 :(得分:0)

尝试此操作(添加if以检查hp是否超过25):

HEAL.hp=HEAL.hp+10;
if (HEAL.hp>25) //add this in your code
HEAL.hp=25;
System.out.println("Your current HP: "+HEAL.hp);
  

如果变量hp超过25,则将其设置为25(因为25是   最高限额)。

答案 3 :(得分:0)

使用Commands

public class CommandGenerator
{
    private Dictionary<string, SqlDbType> _columnToDbType;
    private string _tableName;
    private List<SqlCommand> _commands;

    public CommandGenerator(DataTable table, Dictionary<string, SqlDbType> columnToDbType, string tableName = null)
    {
        _commands = new List<SqlCommand>();
        _columnToDbType = columnToDbType;
        _tableName = (string.IsNullOrEmpty(tableName)) ? tableName : table.TableName;

        table.RowDeleted += table_RowDeleted;
        table.RowChanged += table_RowChanged;
    }

    public IEnumerable<SqlCommand> Commands { get { return _commands; } }

    private void table_RowChanged(object sender, DataRowChangeEventArgs e)
    {
        _commands.Add(GenerateDelete(e.Row));
    }

    private void table_RowDeleted(object sender, DataRowChangeEventArgs e)
    {
        _commands.Add(GenerateDelete(e.Row));
    }

    private SqlCommand GenerateUpdate(DataRow row)
    {

        var table = row.Table;
        var cmd = new SqlCommand();
        var sb = new StringBuilder();
        sb.Append("UPDATE ").Append(_tableName).Append(" SET ");

        var valueColumns = table.Columns.OfType<DataColumn>().Where(c => !table.PrimaryKey.Contains(c));

        AppendColumns(cmd, sb, valueColumns, row);
        sb.Append(" WHERE ");
        AppendColumns(cmd, sb, table.PrimaryKey, row);

        cmd.CommandText = sb.ToString();
        return cmd;
    }

    private SqlCommand GenerateDelete(DataRow row)
    {
        var table = row.Table;
        var cmd = new SqlCommand();
        var sb = new StringBuilder();
        sb.Append("DELETE FROM ").Append(_tableName).Append(" WHERE ");
        AppendColumns(cmd, sb, table.PrimaryKey, row);
        cmd.CommandText = sb.ToString();
        return cmd;
    }

    private void AppendColumns(SqlCommand cmd, StringBuilder sb, IEnumerable<DataColumn> columns, DataRow row)
    {
        foreach (var column in columns)
        {
            sb.Append(column.ColumnName).Append(" = @").AppendLine(column.ColumnName);
            cmd.Parameters.Add("@" + column.ColumnName, _columnToDbType[column.ColumnName]).Value = row[column];
        }
    }
}

Math.min是最大健康状况。

答案 4 :(得分:0)

我在评论中添加了一些适当的解释:

if((HEAL.hp + 10) >= HEAL.maxhp) { // If HEAL.hp after adding 10 would be equal or greater than HEAL.maxhp...
    HEAL.hp = HEAL.maxhp; // Assign HEAL.maxhp value (it's 25 in your example) to HEAL.hp
} else { // Otherwise...
    HEAL.hp = HEAL + 10; // Add 10 to HEAL.hp
}

答案 5 :(得分:0)

这是您应该毫不犹豫地创建自己的数据类型的时刻之一。 从IEEE Software阅读this