java字符串无法转换为布尔错误

时间:2017-04-07 18:59:33

标签: java

我是java的初学者,我似乎无法弄清楚为什么字符串不能转换为布尔值。

错误代码专门出现在" if"线和"否则如果"线。

    String Name, SystemOfMeasure;
    Double Height, IdealWeight;

    // Retrieve user data
    Name = txtName.getText();
    SystemOfMeasure = txtSystemOfMeasure.getText();
    Height = Double.parseDouble(txtHeight.getText());

    if (SystemOfMeasure = "M") {
        IdealWeight = Height * Height * 25;
        lblDisplay.setText(Name + ("'s ideal weight is") + IdealWeight);
    } else if (SystemOfMeasure = "I") {
        IdealWeight = Height * Height * 25 / 703;
        lblDisplay.setText(Name + ("'s ideal weight is") + IdealWeight);
    } else {
        lblDisplay.setText("Please enter M or I");
    }

2 个答案:

答案 0 :(得分:4)

您目前将值M分配给SystemOfMeasure ,但 ifelse if条件需要{{1}表达,不是作业。

因此,您需要使用boolean方法(返回.equals() / true)进行字符串比较,例如false,如下所示:

SystemOfMeasure.equals("M")

另外,请记住,您需要遵循Java命名标准,例如if (SystemOfMeasure.equals("M")) { //add your code } else if (SystemOfMeasure.equals("I") { //add your code } else { //add your code }

等变量名称的小写

答案 1 :(得分:0)

代码有语法错误。 if条件需要是布尔值或表达式,其值为布尔值。

=是Java中的赋值运算符,而==是条件或关系运算符。所以你应该使用==而不是=。

话虽如此,字符串相等性稍微复杂Read StackOverflow answer here,因此您应该使用String.equals()来比较两个字符串。所以正确的代码是:

var characters = [
  {"id": 1, "name": 'Ryu', "country": 'Japan', "tagline":"You must defeat Sheng Long to stand a chance.", "stats":{"power": 7, "speed": 7, "jump": 7, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/3/37/Portrait_SF2_Ryu.png', "character_list_item_backgroundcolor": "#ff9b9b"},
  {"id": 2, "name": 'E. Honda', "country": 'Japan', "tagline":"Can't you do better than that?", "stats":{"power": 9, "speed": 6, "jump": 6, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/5/5a/Portrait_SF2_EHonda.png', "character_list_item_backgroundcolor": "#9bb9ff"},
  {"id": 3, "name": 'Blanka', "country": 'Brazil', "tagline":"Seeing you in action is a joke.", "stats":{"power": 7, "speed": 6, "jump": 6, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/a/ad/Portrait_SF2_Blanka.png', "character_list_item_backgroundcolor": "#a4cc9b"},
  {"id": 4, "name": 'Guile', "country": 'USA', "tagline":"Go home and be a family man.", "stats":{"power": 8, "speed": 8, "jump": 7, "range": 8}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/4/4e/Portrait_SF2_Guile.png', "character_list_item_backgroundcolor": "#f3ff8e"},
  {"id": 5, "name": 'Ken', "country": 'USA', "tagline":"Attack me if you dare, I will crush you.", "stats":{"power": 7, "speed": 7, "jump": 7, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/5/50/Portrait_SF2_Ken.png', "character_list_item_backgroundcolor": "#ff8466"},
  {"id": 6, "name": 'Chun Li', "country": 'China', "tagline":"I'm the strongest woman in the world.", "stats":{"power": 6, "speed": 9, "jump": 9, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/e/e2/Portrait_SF2_ChunLi.png', "character_list_item_backgroundcolor": "#6670ff"},
  {"id": 7, "name": 'Zangief', "country": 'USSR', "tagline":"My strength is much greater than yours.", "stats":{"power": 7, "speed": 5, "jump": 4, "range": 4}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/8/81/Portrait_SF2_Zangief.png', "character_list_item_backgroundcolor": "#ffa551"},
  {"id": 8, "name": 'Dhalsim', "country": 'India', "tagline":"I will meditate and then destroy you.", "stats":{"power": 5, "speed": 4, "jump": 6, "range": 10}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/8/88/Portrait_SF2_Dhalsim.png', "character_list_item_backgroundcolor": "#ffea51"}
];


var MoveListItemComponent = React.createClass({
  render: function () {
     return (
        <div className="move_list_item">
            <p className="character_list_item_country">{this.props.move.move_name}</p>
        </div>
     );
  }
});

var MoveListComponent = React.createClass({
  render: function(){
    var moves = this.props.moveset.map(function(move){
        return(
             <MoveListItemComponent key={move.move_name} move={move} />
        );
    });
    return (
        <div>
            {moves}
        </div>
    );
  }
});

var CharacterPage = React.createClass({

  getInitialState: function() {
    return {character: characters};
  },

  render: function () {
    return (
        <div>
          <h2>Special Moves</h2>
          <p>{this.state.character[0].tagline}</p>
          {
             this.state.character.map( (item, i) => {
                return <MoveListComponent moveset={item.special_moves} />
             })
           }
        </div>
    );
  }
});

ReactDOM.render(<CharacterPage/>, document.getElementById('app'))

但是如果说SystemOfMeasure是一个int,你可以使用

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id='app'/>