Home >Java >javaTutorial >Five conditional statement exercises in Java

Five conditional statement exercises in Java

无忌哥哥
无忌哥哥Original
2018-07-18 10:12:403153browse

1. Enter an integer. If the number is 0, then output "stone". If the number is 1, then output "scissors". If the number is 2, then output "paper". If it is other, then Output "error"

package com.hz.choice;
import java.util.Scanner;
/**
  * 1、输入一个整数,如果此数为0,则输出"石头",
  * 如果此数为1,则输出"剪刀",如果此数为2,则输出"布",
  * 如果为其它,则输出"错误"
  * @author ztw
  *
  */
public class Practice01 {
static int number;
public static void main(String[] args) {
    System.out.println("请输入一个整数:");
    Scanner sc = new Scanner(System.in);
    number = sc.nextInt();
    if(number==0){
        System.out.println("石头");
    }else if(number==1){
        System.out.println("剪刀");
    } else if(number==2){
        System.out.println("布");
    }else{
        System.out.println("错误");
    }
}
}

2. Output "Are you a man?", if the answer is true, then output "So you are a man, haha",
If the answer is: false, then output "Is your gender female?".

package com.hz.choice;
import java.util.Scanner;
/**
 * 2、输出“你是男人吗?”,回答如果是true,
 * 那么输出“原来你是男人呀,呵呵”,
 * 如果回答是:false,那么输出“你的性别难道是女?”。
 * @author ztw
 *
 */
public class Practice02 {
static boolean answer;
public static void main(String[] args) {
    System.out.println("你是男人吗?");
    Scanner sc = new Scanner(System.in);
    answer = sc.nextBoolean();
    if(answer==true){
        System.out.println("原来你是男人呀,呵呵");
    }else{
        System.out.println("你的性别难道是女?");
    }
}
}

3. Enter an integer. If the input number is greater than or equal to 1000, then output "Input number >= 1000", otherwise, output "Input number

package com.hz.choice;
import java.util.Scanner;
/**
 * 3、输入一个整数,如果输入的数大于等于1000,那么输出“输入的数>=1000”,否则的话就输出“输入的数<1000”。
 * @author ztw
 *
 */
public class Practice03 {
static int number;
public static void main(String[] args) {
    System.out.println("输入一个整数:");
    Scanner sc = new Scanner(System.in);
    number = sc.nextInt();
    if(number>=1000){
        System.out.println("输入的数>=1000");
    }else{
        System.out.println("输入的数<1000");
    }
}
}

4. Enter an int type data and determine whether the number is divisible by 2. If it is divisible by 2, then output "This number is an even number", otherwise output "This number is an odd number".
Tips: 8%2==0 10%2==0
9%2=1*/

package com.hz.choice;
import java.util.Scanner;
/**
 * 4、输入一个int型的数据,判断这个数是否能被2整除,
 * 如果能被2整除,那么输出“这个数是偶数”,
 * 否则输出“这个数是奇数”。
 * @author ztw
 *
 */
public class Practice04 {
static int number;
public static void main(String[] args) {
    System.out.println("输入一个整数:");
    Scanner sc = new Scanner(System.in);
    number = sc.nextInt();
    if(number%2==0){
        System.out.println("这个数是偶数");
    }else{
        System.out.println("这个数是奇数");
    }
}
}

5. Enter an int type data and determine whether the number is odd." */

package com.hz.choice;
import java.util.Scanner;
/**
 * 5、输入一个int型的数据,判断这个数是否是奇数”
 * @author ztw
 *
 */
public class Practice05 {
static int number;
public static void main(String[] args) {
    System.out.println("输入一个整数:");
    Scanner sc = new Scanner(System.in);
    number = sc.nextInt();
    if(number%2==0){
        System.out.println("这个数不是奇数!!");
    }else{
        System.out.println("这个数是奇数!!!");
    }
}
}

The above is the detailed content of Five conditional statement exercises in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn