1、键盘录入两个数据,并对这两个数据求和,输出其结果
代码:
package _01.homework_键盘录入;
import java.util.Scanner ;
public class ScannerDemo {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请您输入一个数据");
int number1 = sc.nextInt();
System.out.println("请您输入一个数据");
int number2 = sc.nextInt();
int add = number1 + number2;
System.out.println("您输入的数据和是:"+add);
}
}
2、键盘录入练习:键盘录入两个数据,获取这两个数据中的最大值
代码:
package _01.homework_键盘录入;
import java.util.Scanner;
public class ScannerDemo2 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请您输入一个数据");
int number1 = sc.nextInt();
System.out.println("请您输入一个数据");
int number2 = sc.nextInt();
int max = (number1 >number2) ? number1: number2;
System.out.println("您输入的数据中最大的是:"+max);
}
}
3、键盘录入三个数据,获取这三个数据中的最大值
代码:
package _01.homework_键盘录入;
import java.util.Scanner;
public class ScannerDemo3 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请您输入一个数据");
int number1 = sc.nextInt();
System.out.println("请您输入一个数据");
int number2 = sc.nextInt();
System.out.println("请您输入一个数据");
int number3 = sc.nextInt();
int max = (number1 >number2) ? (number1>number3?number1:number3): (number2 >number3? number2 :number3);
System.out.println("您输入的数据中最大的是:"+max);
}
}
4、键盘录入两个数据,比较这两个数据是否相等
代码:
package _01.homework_键盘录入;
import java.util.Scanner;
public class ScannerDemo4 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请您输入一个数据");
int number1 = sc.nextInt();
System.out.println("请您输入一个数据");
int number2 = sc.nextInt();
boolean flag = number1 == number2 ? true : false ;
System.out.println(flag);
}
}