如何从键盘获取不同类型的变量:Scanner类
具体实现步骤:
1.导包:import java.util.Scanner
代码:
import java.util.Scanner;
class ScannerTest{
public static void main(String[] args){
}
2.Scanner的实例化:Scanner scan=new Scanner(System.in);
代码:
import java.util.Scanner;
class ScannerTest{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
}
3.调用Scanner类的相关方法,(next()/nextXxx())来获取指定类型的变量
import java.util.Scanner;
class ScannerTest{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
int num=scan.nextInt();
System.out.println(num);
}
}
代码:
import java.util.Scanner;
class ScannerTest{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name=scan.next();
System.out.println(name);
}
}
String 类型的获取
代码:
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.println("请输入姓名:");
String name=scan.next();
System.out.println(name);
System.out.println("芳龄几何:");
int age=scan.nextInt();
System.out.println(age);
System.out.println("体重几吨:");
double weight=scan.nextDouble();
System.out.println(weight);
System.out.println("你是否和我看对眼了呢?(true/false)");
boolean isLove=scan.nextBoolean();
System.out.println(isLove);
//对于char类型的获取,Scanner没有提供相关的方法,只能获取一个字符串
System.out.println("请输入性别:(男/女)");
String gender=scan.next();//男
char genderChar=gender.charAt(0);//获取索引为0位置上的字符
System.out.println(genderChar);
代码:
import java.util.Scanner;
class IfTest{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
Scanner.out.println("请输入期末成绩");
int score=scan.nextInt();
if(score==100){
System.out.println("一辆BMW");
}else if(score>80&&score<=90){
System.out.println("iphone");
}else if(score>=60&&score<=80){
System.out.println("iPad");
}else{
System.out.println("NOTHING");
}
}
}
代码
class IfTest2{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入第一个整数:");
int num1=scanner.nextInt();
System.out.println("请输入第二个整数:");
int num2=scanner.nextInt();
System.out.println("请输入第三个整数:");
int num1=scanner.nextInt();
if(num1>=num2){
if(num3>=num1){
System.out.println(num2+","+num1+","+num3);
}else if(num3<=num2){
System.out.println(num3+","+num2+","+num1);
}else{
System.out.println(num2+","+num3+","+num1);
}
}
}else{
if(num3>=num2){
System.out.println(num1+","+num2+","+num3);
}else if(num3<=num1){
System.out.println(num3+","+num1+","+num2);
}else{
System.out.println(num1+","+num3+","+num2);
}
}
代码:
int dogAge=6;
if(dogAge>=0&&dogAge<=2){
System.out.println("相当于人的年龄:"+dogAge*10.5);
}else if(dogAge>2){
System.out.println("相当于人的年龄:"+(2*10.5+(dogAge*2)-4));
}else{
System.out.println("狗狗没出生");
}
代码:
class TestCaiPiao{
public static void main(String[] args){
//1.随机产生一个两位数
//System.out.println(Math.random());//产生[0,1)
int number=(int)(Math.random()*90+10);//得到[10,99]即[10,100)
//System.out.println(number);
int numberShi=number/10;
int numberGe=number%10;
//用户输入一个两位数
Scanner input=new Scanner(System.in);
System.out.print("请输入一个两位数:");
int guess=input.nextInt();
int guessShi=guess/10;
int guessGe=guess%10;
if(number==guess){
System.out.println("奖金1w美元");
}else if(numberShi==guessGe&&numberGe==guessShi){
System.out.println("奖金3k美元");
}else if(numberShi==guessShi||numberGe==guessGe){
System.out.println("奖金1k美元");
}else if(numberShi==guessGe||numberGe==guessShi){
System.out.println("奖金5百美元");
}else{
System.out.println("没中奖");
}
System.out.println("中奖号码是:"+number);
}
产生随机数,以及关于包含不包含的边界问题的公式计算
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)