结合布尔值和if

时间:2017-03-27 17:32:26

标签: java if-statement boolean

我刚刚开始使用Java。我想写一个代码,要求用户在两种语言之间做出选择:英语和西班牙语。 用户将键入ENG并且应该打印“Hello my friend”或者用户将键入ESP并且应该打印“Ola amigo”。如果用户会输入其他内容,那么问题就会出现再次正确的选择。下面是我的代码到目前为止..你能帮忙吗?

`

public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        System.out.println("Make a language choice: ENG for ENGLISH or ESP for SPANISH");


        boolean ENG;  // determines English language
        boolean ESP;  //determines Spanish language

        if(ENG) {

            System.out.println("Hello my friend");
        }

        if(ESP) {
            System.out.println("Ola amigo");
        }

        else {
            System.out.println ("Make a language choice: ENGLISH or SPANISH");
        }

`

2 个答案:

答案 0 :(得分:1)

尝试使用while()循环进行输入验证

          <div id="Kati1">
          

            <a id="101" class=" RoomStyle" onclick="RoomClick(1,101)">101</a>
            <a id="102" class=" RoomStyle" onclick="RoomClick(1,102)">102</a>
            <a id="103" class=" RoomStyle" onclick="RoomClick(1,103)">103</a>
            <a id="104" class=" RoomStyle" onclick="RoomClick(1,104)">104</a>
            <a id="105" class=" RoomStyle" onclick="RoomClick(1,105)">105</a>
         
            <a id="201" class=" RoomStyle" onclick="RoomClick(1,201)">201</a>
            <a id="202" class=" RoomStyle" onclick="RoomClick(1,202)">202</a>
            <a id="203" class=" RoomStyle" onclick="RoomClick(1,203)">203</a>
            <a id="204" class=" RoomStyle" onclick="RoomClick(1,204)">204</a>
            <a id="205" class=" RoomStyle" onclick="RoomClick(1,205)">205</a>
         
            <a id="301" class=" RoomStyle" onclick="RoomClick(1,301)">301</a>
            <a id="302" class=" RoomStyle" onclick="RoomClick(1,302)">302</a>
            <a id="303" class=" RoomStyle" onclick="RoomClick(1,303)">303</a>
            <a id="304" class=" RoomStyle" onclick="RoomClick(1,304)">304</a>
            <a id="305" class=" RoomStyle" onclick="RoomClick(1,305)">305</a>
</div>

答案 1 :(得分:1)

String answer = scanner.next();
boolean ENG = ("ENG").equals(answer);  // determines English language
boolean ESP = ("ESP").equals(answer);  //determines Spanish language